i have logging configured in log4j.properties file , i am getting a output something like this
2013-11-12 19:33:17,897 - INFO Starting queue dispatching for DaphneStore Queue: om.fi
2013-11-12 19:33:17,897 - INFO Starting CBR queue dispatching for DaphneStore
2013-11-12 19:33:17,897 - INFO Starting server shutdown
is there any possibility that i do not get timestamp information on the left hand side for some of the lines, something like this
2013-11-12 19:33:17,897 - INFO Starting queue dispatching for DaphneStore Queue:
Starting CBR queue dispatching for DaphneStore
Starting server shutdown
here is my log4j configuration
# Set root logger to output only ERROR and FATAL events to R appender
log4j.rootLogger=ERROR, R
# Define R appender to output to local log
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.file=D:/logs/abc.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %-5p %c - %m%n
Couple of quick solutions:
Create a different appender with ConversionPattern=%m%n (This will just print the logger text without timestamps I suppose).
Modify the exisitng appender with ConversionPattern=%m%n. Here you can manually add timestamps (using DateFormat like SimpleDateFormat) to the logger text wherever needed. In rest of the cases, plain text will be added to the logger file without timestamp.
You are probably not using log4j correctly. Every call to log.info/error/...() is considered a separate log message.
What you are probably trying to do is something like this:
final StringBuilder logmsg = new StringBuilder();
logmsg.append("Starting queue dispatching for DaphneStore Queue: \n");
logmsg.append("Starting CBR queue dispatching for DaphneStore\n");
logmsg.append("Starting server shutdown\n");
log.info(logmsg);
UPDATE
What I mean is that the timestamp is an important part of a log message, it says when something happened. If you don't need this information all the time you should probably lose the timestamp all-together and put a single logging call to mark when your application was started:
String moment = (new SimpleDateFormat("yyyy-MM-dd ...")).format(new Date());
log.info(moment + " Application started");
Related
I created 2 PatternLayoutEncoder in my program logger with different fields, 1 for info and debug messages and 2 for warning and error messages.
ThresholdFilter consoleFilter = new ThresholdFilter();
consoleFilter.setLevel("INFO");
consoleFilter.start();
consoleAppenderError.addFilter(consoleFilter);
ThresholdFilter consoleFilterError = new ThresholdFilter();
consoleFilterError.setLevel("WARN");
consoleFilterError.start();
consoleAppenderError.addFilter(consoleFilterError);
The info logger print also the warning and error messages, and I get double messages.
Is there an option to set specific level so that the info logger only receives info and debug messages without error and warning massages?
How can multiple log levels added to the same log file in log4j?
For example:
log4j.rootLogger=INFO,WARN,stdout,file
It gives the log4j error when application start as:
Could not instantiate appender named WARN.
The purpose of the threshold is to tell log4j to ignore all logging requests with a priority lower than what you specify. Specifying a given threshold does not limit you to logging with that threshold.
FileAppender fa = new FileAppender();
fa.setThreshold(Level.INFO);
fa.setAppend(true);
Logger.getRootLogger().addAppender(fa);
In the above code, the appender has been configured to operate with a threshold of INFO. This means that the following code will not log, because DEBUG is a lower priority than INFO:
Logger logger = Logger.getLogger(SomeClass.class);
logger.debug("This will not log");
But this code will log:
logger.warn("This debug message will log.");
logger.error("And this error message will also log.");
In this case, both WARN and ERROR have a higher priority than INFO.
How to know in log4j , that ERROR level is triggered. In my code I have written like If an exception occurs i.e if an ERROR level is triggered then I will show log file path in console and wont show any message for other levels.
In Log4j you can configure different levels in the log4j.properties or xml file which ever you choose to use.
There is some predefined pattern with the error levels and runs from lower to upper.
Lowest id Debug its in this sequence.
1.Debug
2. Warn
3. Error etc
So if you want only Error messages then configure the Error level to Error. you wot get Debug and Warn
If you configure Debug you get everything
Ex.
<logger name="org.springframework...">
<level value="error" />
</logger>
This is the point of separating the code from the configuration.
E.g., your code could look like this:
try {
someMethod();
} catch (SomeException e) {
log.error("Error!!! " + e.getMessage());
}
And in your log4j configuration, your configure your log to output only ERROR level messages:
log4j.rootLogger=error, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# Just an example of a pattern layout
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
First some background. I have a batch-type java process run from a DOS batch script. All the java logging goes to stdout, and the batch script redirects the stdout to a file. (This is good for me because I can ECHO from the script and it gets into the log file, so I can see all the java JVM command line args, which is great for debugging.) I may not
I use slf4j API, and for the backend I used to use log4j, but recently switched to logback-classic.
Although all my application code uses slf4j, I have a third party library that does it's own logging (not using a standard API) which also gets written to stdout.
The problem is that sometimes log lines get mixed up and don't cleanly appear on separate lines. Here is an example of some messed up output:
2010-05-28 18:00:44.783 [thread-1 ] INFO CreditCorrelationElementBuilderImpl - Bump parameters exist for scenario, now attempting bumping. [indexDisplayName=STANDARD_S1_v300]
2010-05-28 18:01:43.517 [thread-1 ] INFO CreditCorrelationElementBuilderImpl - Found adjusted point in data, now applying bump. [point=0.144040000000000]
2010-05-28 18:01:58.642 [thread-1 ] DEBUG com.company.request.Request - Generated request for [dealName=XXX_20050225_01[5],dealType=GENERIC_XXX,correlationType=2,copulaType=1] in 73.8 s, Simon Stopwatch: [sys1.batchpricer.reqgen.gen INHERIT] total 1049 s, counter 24, max 74.1 s, min 212 ms
2010-05-28 18:05/28/10 18:02:20.236 INFO: [ServiceEvent] SubmittedTask:BC-STRESS_04_FZBC-2010-05-21-545024448189310126-23
01:58.658 [req-writer-2b ] INFO .c.g.r.o.OptionalFileDocumentOutput - Writing request XML to \\filserver\dir\file1.xml - write time: 21.4 ms - Simon Stopwatch: [sys1.batchpricer.reqgen.writeinputfile INHERIT] total 905 ms, counter 24, max 109 ms, min 10.8 ms
2010-05-28 18:02:33.626 [ResponseCallbacks-1: DriverJobSpace$TakeJobRunner$1] ERROR c.c.s.s.D.CalculatorCallback - Id:23 no deal found !!
2010-0505/28/10 18:02:50.267 INFO: [ServiceEvent] CompletedTask:BC-STRESS_04_FZBC-2010-05-21-545024448189310126-23:Total:24
Now comparing back to older log files, it seems the problem didn't occur when using log4j as the logging backend. So logback must be doing something different.
The problem seems to be that although PrintStream.write(byte buf[], int off, int len) is synchronized, however I can see in ch.qos.logback.core.joran.spi.ConsoleTarget that System.out.write(int b) is the only write method called.
So inbetween logback outputting each byte, the thirdparty library is managing to write a whole string to the stdout. (Not only is this cause me a problem, but it must also be a little inefficient?)
Is there any other fix to this interleaving problem than patching the code to ConsoleTarget so it implments the other write methods? Any nice work arounds. Or should I just file a bug report?
Here is my logback.xml:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-16thread] %-5level %-35.35logger{30} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
I'm using logback 0.9.20 with java 1.6.0_07.
In such case I would go through System.setOut(PrintStream out) for the given 3rd party lib that does not behave. Implement a thread that would read this stream of log split it it say by new line and spit it to the logging solution that you use.
Just be careful not to start reading and writing to the same thread :-)
this is what you do:
You get the System.out stream save it aside
You configure your logger to use this stream see OutputStreamAppender
You create a thread that drains a stream that you assign as new System.out (your 3rd party lib will write there) and send well formatted output to the log
You got yourself a pretty log that in sync more or less with what happens in the system
In the end fixing the underlying issue was easier than any workaround.
Patch for fix: http://gist.github.com/434516
Bug raise in logback jira: http://jira.qos.ch/browse/LBCORE-158
It looks like you have two different log configurations writing to the STDOUT. The pattern of these two seems to be very different when trying to decode the mess:
2010-05-28 18:01:58.658 [req-writer-2b ] INFO .c.g.r.o.OptionalFileDocumentOutput - Writing request XML to \\filserver\dir\file1.xml - write time: 21.4 ms - Simon Stopwatch: [sys1.batchpricer.reqgen.writeinputfile INHERIT] total 905 ms, counter 24, max 109 ms, min 10.8 ms
05/28/10 18:02:20.236 INFO: [ServiceEvent] SubmittedTask:BC-STRESS_04_FZBC-2010-05-21-545024448189310126-23
The second line seems to be using the default pattern in stead of your definition. Is there a logger loaded somewhere that uses the default configuration instead of your XML configuration?
Is it possible to configure different log levels for a single Logger based on the appender?
I realize this is similar to this question, and this is as far as I had already got myself, but the problem with this is that the threshold applies to all loggers that log to that appender, whereas I only want the threshold to apply to a single logger.
i.e. So far I have something like this:
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=WARN
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.logger.mylogger=DEBUG,logfile
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.DatePattern=${roll.pattern.daily}
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %C{2} - %m%n
log4j.appender.logfile.File=mylogfile.log
I want mylogger DEBUG messages to be send to the logfile appender, but I also want mylogger INFO messages to be sent to the stdout appender (but for all other loggers only WARN ings). Using the Threshold to limit stdout to WARN restricts the output of mylogger.
Aha, I fixed it by changing
log4j.appender.stdout.Threshold=WARN
to
log4j.appender.stdout.Threshold=INFO
Should have been more careful first time round.