How to create a log structure for a Java application
I'm new to java, I know Java has Log4J, logback, etc. for logging. My question is more about how many log files we should have in our application. It should be for a thread, for a thread group, a process, an exception, etc. In our application, it is possible to have a large number of threads and think about the disadvantages of having a log file in the thread. Are there any best practices for registering in applications with huge number of threads.
Thanks in advance!
source to share
1 message log - name it SystemOut.log
1 log for stack trace - call it SystemErr.log
1 log for traces - name it Trace.log
1 log for inline stdout - call it nativeStdOut.log
1 log for native stderr - Call it nativeStdErr.log
You have a settings panel that sets:
maxSize
maxCount
When the log reaches its maximum size, start rewinding them to maxCount and add a timestamp to the rolled filename.
source to share
Usually there is one log file for each application (process) - rarely Thread
and never Exception
. Sometimes this log file is split into different log levels: debug messages in one bucket, information in another, warnings / errors in a third. This makes it easier to view errors by just looking at the warning and criticism file.
log4j has a config file where you can redirect specific messages to specific files using different criteria. Here's a sample log4j properties file:
# default is WARN and above going to the appender "logfile" and STDOUT
log4j.rootLogger=WARN, logfile, stdout
# write to ui.log for a day and then move to .yyyy-MM-dd suffix
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=ui.log
log4j.appender.logfile.Append=true
log4j.appender.logfile.DatePattern='.'yyyy-MM-dd
# but we log information message from classes in the package com.mprew.be
log4j.logger.com.mprew.be=INFO
log4j and custom logs, decorate name Class
, priority level, date / time, etc. every time . For example:
# date time priority Class-name Log message
2012-03-26 00:55:39,545 [INFO] CmsClientTask Content is up-to-date
Usually, exceptions are logged as multiple lines, so you can get the entire stack trace.
2012-03-26 01:55:35,777 [INFO] ExceptionInterceptor Reporting problem to customer
org.springframework.NoSuchRequestException: No request handling method
at com.ui.base.BaseController.invokeNamedHandler(BaseController.java:240)
at com.ui.base.BaseController.handleRequestInternal(BaseController.java:100)
at com.ui.base.CoreServices.handleRequest(CoreServicesController.java:147)
...
In our distributed system, we route all logs from the whole system to 2 servers that write debug, information and warning logs. Along with date / time, class name, priority and message, the log messages also have a hostname and a specific log token so that we can easily identify the problem classes. In one line:
2012-03-26 00:00:00.045 UTC INFO FE8 TrayController TRAY_CLIENT_LOOKUP
message=created unknown client with partner STW9
Then we can grep easily for specific problems.
Hope it helps.
source to share