How do I change dropwizard's access log format? - java

I want to output the access logs to console. I also want to log files with the following configuration:
server:
requestLog:
appenders:
- type: file
currentLogFilename: D:/Log/access.log
archivedLogFilenamePattern: D:/Log/access-%d.log.gz
logFormat: "%d{yyyy-MM-dd HH:mm:ss.SSS,Asia/Shanghai} %-6level [%t] %c %msg %n"
- type: console
logFormat: "%d{yyyy-MM-dd HH:mm:ss.SSS,Asia/Shanghai} %-6level [%t] %c %msg %n"
template: Hello, %s!
defaultName: Stranger
I can run my application normally, but the logformat output is not like my configuration in the configuration file. I can rewrite the class using the org.eclipse.jetty.server.AbstractNCSARequestLog's log method, but obviously this is a very bad method. It solves the problems but if I want to upgrade my application I have to modify all of my AbstractNCSARequestlog class, which is very troublesome and messy.
What is another way I can go about this problem?

Related

Why does my log4j2 properties configuration not log to console nor a separate file?

I have been trying to log messages from a specific class. I only want the specific messages from that class to log to a separate file and nothing else. I cant seem to get it right and I have tried multiple configurations including XML format. Id be helpful for feedback on my configuration. (see Below )
log4j2.appender.solrindexlog.type=RollingFile
log4j2.appender.solrindexlog.name=SolrIndexLog
log4j2.appender.solrindexlog.fileName=${NAME_LOG_DIR}/tomcat/solrindex.log
log4j2.appender.solrindexlog.filePattern=${NAME_LOG_DIR}/tomcat/solrindexlog-%d{yyyyMMdd}.log
log4j2.appender.solrindexlog.layout.type=PatternLayout
log4j2.appender.solrindexlog.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
log4j2.appender.solrindexlog.policies.type=Policies
log4j2.appender.solrindexlog.policies.time.type=TimeBasedTriggeringPolicy
log4j2.appender.solrindexlog.policies.time.interval=2
log4j2.appender.solrindexlog.policies.time.modulate=true
log4j2.appender.solrindexlog.policies.size.type=SizeBasedTriggeringPolicy
log4j2.appender.solrindexlog.policies.size.size=100MB
log4j2.appender.solrindexlog.strategy.type=DefaultRolloverStrategy
log4j2.appender.solrindexlog.strategy.max=5
log4j2.logger.solrindexlog.name=se.package.name
log4j2.logger.solrindexlog.level=debug
log4j2.logger.solrindexlog.appenderRefs=stdout
log4j2.logger.solrindexlog.appenderRef.stdout.ref=STDOUT
log4j2.rootLogger.level=info
log4j2.rootLogger.appenderRef.stdout.ref=STDOUT
log4j2.rootLogger.appenderRef.solrindexlog.ref=SolrIndexLog
log4j2.rootLogger.appenderRefs=stdout,solrindexlog
Try below configuration file -
appender.solrindexlog.type=RollingFile
appender.solrindexlog.name=SolrIndexLog
appender.solrindexlog.fileName=${NAME_LOG_DIR}/tomcat/solrindex.log
appender.solrindexlog.filePattern=${NAME_LOG_DIR}/tomcat/solrindexlog-%d{yyyyMMdd}.log
appender.solrindexlog.layout.type=PatternLayout
appender.solrindexlog.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.solrindexlog.policies.type=Policies
appender.solrindexlog.policies.time.type=TimeBasedTriggeringPolicy
appender.solrindexlog.policies.time.interval=2
appender.solrindexlog.policies.time.modulate=true
appender.solrindexlog.policies.size.type=SizeBasedTriggeringPolicy
appender.solrindexlog.policies.size.size=100MB
appender.solrindexlog.strategy.type=DefaultRolloverStrategy
appender.solrindexlog.strategy.max=5
logger.se.package.name.name=se.package.name
logger.se.package.name.level=debug
logger.se.package.name.additivity = false
logger.se.package.name.appenderRef.solrindexlog.ref= SolrIndexLog
logger.se.package.name.appenderRef.stdout.ref= STDOUT
rootLogger.level=info
rootLogger.additivity = false
rootLogger.appenderRef.solrindexlog.ref= SolrIndexLog
rootLogger.appenderRef.stdout.ref= STDOUT
Please change other parts of the configuration file that are not written in above configuration file like ConsoleAppender etc.
Also, as per this configuration file, all logs message will go in file as well as in console.

how to find out the full name of Java class in DropWizard logs

We are using DropWizard in a Java based micro-service. In the logs, we have lines like:
[dw-1000 - POST myservice/endpoint] c.s.x.m.s.c.p2p - my error message
My question is: how can I find out the full names of the Java class path represented as "c.s.x.m.s.c.p2p" in the above log message?
Thanks.
You need to change the default configuration in your yaml:
The logFormat is the key to your solution - %c displays the full logger name, and if there is no number followed, it's the full name with no name-cuts:
logging:
appenders:
- type: file
...
logFormat: '[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] [%c] [%thread] [-[%msg]-] %n'

how to make Log4j configuration properly

I am new to the Log4j framework and after reading some stuff i got some fare idea about logging mechanism, but still
i have some doubt about the following properties.
log4j.category.com.cloud.sample=INFO, file, C
log4j.additivity.com.cloud.sample=true
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.Target=System.out
log4j.appender.C.ImmediateFlush=true
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%-5p %d [%t] %m%n
#log4j.rootLogger=INFO, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
### direct messages to file ###
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/var/basic/logs/sample.log
log4j.appender.file.Append=true
log4j.appender.file.MaxFileSize=10MB
# mylog.log.10 \u307e\u3067\u4fdd\u6301
log4j.appender.file.MaxBackupIndex=50
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p [%t] %c{1} - %m%n
log4j.rootLogger=INFO, C, file
In the first line of the above code contains two appenders(file, C) after that we will be having appender for both file and C. So as per my understanding logs will of category will be stored to Console and sample.log. Please let me know if i am wrong.
log4j.rootLogger=INFO, A1 and respective properties are not used right?
log4j.rootLogger=INFO, C, file: This line is about root logger, I think in my case it is not useful, because it is defined at the last line and there is no properties defined over here.
Please could any body confirm my understanding and suggest me if any changes required in the above configuration
The root logger resides at the top of the logger hierarchy. It is exceptional in three ways:
it always exists,
its level cannot be set to null
it cannot be retrieved by name.
The rootLogger is the father of all appenders. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy (including rootLogger).

log4j not logging to some files

I've seen many questions concerning log4j not writing any files, but my problem is that, log4j does (seem to) work, but not for all files.
My configuration is as follows (I have omitted a few loggers for simplicity, the below lines appear in the order they are in in the file):
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
# this logger works without any problems
log4j.appender.errorFile=org.apache.log4j.RollingFileAppender
log4j.appender.errorFile.File=${LOG_HOME}/errors.log
log4j.appender.errorFile.MaxFileSize=20MB
log4j.appender.errorFile.MaxBackupIndex=10
log4j.appender.errorFile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorFile.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
log4j.appender.errorFile.Threshold=ERROR
# this file is created but always empty
log4j.appender.barcodeScanner=org.apache.log4j.RollingFileAppender
log4j.appender.barcodeScanner.File=${LOG_HOME}/barcodeScanner.log
log4j.appender.barcodeScanner.MaxFileSize=20MB
log4j.appender.barcodeScanner.MaxBackupIndex=5
log4j.appender.barcodeScanner.layout=org.apache.log4j.PatternLayout
log4j.appender.barcodeScanner.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
# this logger also works
log4j.appender.DoubleDtpaList=org.apache.log4j.RollingFileAppender
log4j.appender.DoubleDtpaList.File=${LOG_HOME}/DoubleDtpaList.log
log4j.appender.DoubleDtpaList.MaxFileSize=20MB
log4j.appender.DoubleDtpaList.MaxBackupIndex=1
log4j.appender.DoubleDtpaList.layout=org.apache.log4j.PatternLayout
log4j.appender.DoubleDtpaList.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
log4j.rootLogger=error, errorFile
log4j.logger.scanner.BarcodeScanner=debug, barcodeScanner
log4j.logger.util.DetectDoubles=trace, DoubleDtpaList
Using the logger within the BarcodeScanner class yields correct results when using logger.error() (this logs to the errors.log file), but all calls to logger.debug() and logger.info() have no effect.
The variable LOG_HOME is passed as an argument/option when executing the *.jar file.
EDIT 1: I added another (working) logger/appender configuration for clarification. Another piece of information I unfortunately left out ist that I want all logs for the scanner to go to the barcodeScanner.log file (with the errors additionally going to errors.log (root Logger)

Overwriting log4j not working

I have a log4j properties with the following configuration:
log4j.appender.LOG=org.apache.log4j.RollingFileAppender
log4j.appender.LOG.File=${directory}/log/app.log
log4j.appender.LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG.layout.ConversionPattern=%d{dd MMM HH:mm:ss,SSS} %-5p [%c{1}] %m%n
log4j.appender.LOG.Threshold=DEBUG
log4j.appender.LOG.append=false
log4j.appender.LOG.bufferedIO=false
log4j.appender.LOGHISTORY=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOGHISTORY.File=${directory}/log/old-logs/app.log
log4j.appender.LOGHISTORY.File.DatePattern='.'yyyy-MM-dd
log4j.appender.LOGHISTORY.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGHISTORY.layout.ConversionPattern=%d{dd MMM HH:mm:ss,SSS} %-5p [%c{1}] %m%n
log4j.appender.LOGHISTORY.Threshold=DEBUG
log4j.appender.LOGHISTORY.append=true
log4j.appender.LOGHISTORY.bufferedIO=false
I want to save a history of previous day's logs in the "log/old-logs" folder. This is working great with the DailyRollingFileAppender.
I also want to have a log of the current day in the "log" folder. This is working fine on my localhost (Windows + Eclipse + Geronimo), but is not working properly on my testing server (Linux + WebSphere). In this case the "app.log" is not being overwritten and everything is being appended at the end of the log.
AFAIK Rolling file appender will only roll the file when a specified a max size is reached .
The above scenario might be working for you in the local copy each time application is restarted, the log file will overwritten because of .append = false . On a production environment I do not think that the server is restarted that is why the file is written as per .append = false . Could this be your case why overwriting did not work ?
Check this.
You should use Append with a capital A.
log4j.appender.LOG.Append=false
log4j.appender.LOGHISTORY.Append=true
For the RollingFileAppender, you need to specify the property "MaxFileSize". This will tell log4j to roll the file over when it reaches that size.
eg: Below will roll over when log file size reaches 2MB
log4j.appender.LOG=org.apache.log4j.RollingFileAppender
log4j.appender.LOG.File=${directory}/log/app.log
log4j.appender.LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG.layout.ConversionPattern=%d{dd MMM HH:mm:ss,SSS} %-5p [%c{1}] %m%n
log4j.appender.LOG.Threshold=DEBUG
log4j.appender.LOG.MaxFileSize=2MB
log4j.appender.LOG.MaxBackupIndex=2 (This is optional, tells log4j the maximum backup files to take)

Categories