How can I make Apache httpcomponents use log4j?
-
11-12-2019 - |
Question
My log4j.properties file looks like this:
# Root logger option
log4j.rootLogger=DEBUG, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/home/user/logs/myapp.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%5p [%c] %m%n
log4j.logger.org.apache.http=DEBUG
The file myapp.log is created, and my http requests are executing, but the log file remains empty and I have no idea why.
edit: in the class that is using HttpClient, I was able to log messages there. So it makes me think the log4j.logger.org.apache.http line is wrong even though that's what the documentation on http://hc.apache.org/ tells you to do.
Solution
I had a similar problem in my Grails app and it required that I declare my config in a specific order. I had to declare my debug filters, my appenders and then my root loggers in that specific order. I included my config below for your reference. I hope this helps you.
def logLayoutPattern = new PatternLayout("%d [%t] %-5p %c %x - %m%n")
debug 'grails.app.controllers.com.sagebauer'
debug 'grails.app.services.com.sagebauer'
error 'org.codehaus.groovy.grails.web.servlet', // controllers
'org.codehaus.groovy.grails.web.pages', // GSP
'org.codehaus.groovy.grails.web.sitemesh', // layouts
'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
'org.codehaus.groovy.grails.web.mapping', // URL mapping
'org.codehaus.groovy.grails.commons', // core / classloading
'org.codehaus.groovy.grails.plugins', // plugins
'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
'org.springframework',
'org.hibernate',
'net.sf.ehcache.hibernate'
appenders {
appender new ConsoleAppender(name: "console",
threshold: log4jConsoleLogLevel,
layout: logLayoutPattern
)
}
root {
error 'console'
additivity = true
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow