?
Solved

logging to two different files with log4php

Posted on 2008-11-17
2
Medium Priority
?
1,072 Views
Last Modified: 2012-05-05
Is there a way to make log4php write to two different files, and keep those separate from each other, based on the log level?

I managed to write INFO data to a file, and ERROR data to another using the property file below. But the problem is
that when I ask log4php to write to the ERROR log, it also prints it in the INFO one. Please take a look and let me know if
there is a way not to write to INFO appender when I specify ERROR:


Configuration file:

# Setup for the summary log file:
log4php.appender.A1=LoggerAppenderDailyFile
log4php.appender.A1.MaxFileSize=1MB
log4php.appender.A1.MaxBackupIndex=1
log4php.appender.A1.layout=LoggerLayoutTTCC
log4php.appender.A1.layout.ContextPrinting="true"
log4php.appender.A1.layout.DateFormat="%c"
log4php.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4php.appender.A1.File=logs/log_summary_%s.log
log4php.appender.A1.Threshold=INFO
# NOTE: The commented out lines didn't do the trick, I was just copying from the java implementation...
#log4php.appender.A1.varia.LevelRangeFilter.LevelMin=INFO
#log4php.appender.A1.varia.LevelRangeFilter.LevelMax=INFO

# Setup for the errors log file:
log4php.appender.A2=LoggerAppenderDailyFile
log4php.appender.A2.MaxFileSize=1MB
log4php.appender.A2.MaxBackupIndex=1
log4php.appender.A2.layout=LoggerLayoutTTCC
log4php.appender.A2.layout.ContextPrinting="true"
log4php.appender.A2.layout.DateFormat="%c"
log4php.appender.A2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4php.appender.A2.File=logs/log_errors_%s.log
log4php.appender.A2.Threshold=ERROR
# NOTE: The commented out lines didn't do the trick, I was just copying from the java implementation...
#log4php.appender.A2.varia.LevelRangeFilter.LevelMin=ERROR
#log4php.appender.A2.varia.LevelRangeFilter.LevelMax=ERROR


log4php.rootLogger=debug, A1, A2


And the way I am calling log4php:


// Summary logging (works, doesn't write to error log, only to summary log as expected):
$logger = LoggerManager::getLogger('myFunctionHere');
$logger->info($data_to_log_here);

// Error logging (works, but writes to both summary and error logs, I only want this to go to the error file):
$logger = LoggerManager::getLogger('myFunctionHere');
$logger->error($data_to_log_here);
0
Comment
Question by:cabrera48
2 Comments
 
LVL 18

Accepted Solution

by:
Matthew Kelly earned 2000 total points
ID: 22980677
I think it probably has something to do with the appender additivity flag defaulting to true. Try splitting the two appenders into different categories, or setting additivity to false.

http://logging.apache.org/log4j/1.2/manual.html

Appender Additivity
The output of a log statement of logger C will go to all the appenders in C and its ancestors. This is the meaning of the term "appender additivity".

However, if an ancestor of logger C, say P, has the additivity flag set to false, then C's output will be directed to all the appenders in C and it's ancestors upto and including P but not the appenders in any of the ancestors of P.

Loggers have their additivity flag set to true by default
0
 

Author Comment

by:cabrera48
ID: 22980951
That was it, thanks matthewstevenkelly,
The coded solution below just in case anybody else needs it later:

# Setup for the summary log file:

log4php.appender.A1=LoggerAppenderDailyFile
log4php.appender.A1.MaxFileSize=1MB
log4php.appender.A1.MaxBackupIndex=10
log4php.appender.A1.layout=LoggerLayoutTTCC
log4php.appender.A1.layout.DateFormat="%c"
log4php.appender.A1.File=logs/summary/log_summary_%s.log
log4php.appender.A1.Threshold=INFO

log4php.rootLogger=DEBUG, A1
log4php.additivity.rootLogger="false"

# Setup for the errors log file:
log4php.logger.errorLogger = ERROR, A2
log4php.additivity.errorLogger="false"

log4php.appender.A2=LoggerAppenderDailyFile
log4php.appender.A2.MaxFileSize=1MB
log4php.appender.A2.MaxBackupIndex=1
log4php.appender.A2.layout=LoggerLayoutTTCC
log4php.appender.A2.layout.DateFormat="%c"
log4php.appender.A2.File=logs/errors/log_errors_%s.log
log4php.appender.A2.Threshold=ERROR
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question