Glassfish LOG4J loggin diferent applications - java

I have many applications (war, ejb) in same glassfish server, the applications have different log4j configuration
app1.war -> log4j.properties
log4j.rootLogger=info, infcore
log4j.appender.infcore=org.apache.log4j.RollingFileAppender
log4j.appender.infcore.File=/var/log/app1info.log
log4j.appender.infcore.MaxFileSize=20000KB
log4j.appender.infcore.MaxBackupIndex=20
log4j.appender.infcore.layout=org.apache.log4j.PatternLayout
log4j.appender.infcore.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] [%t] %c %x - %m%n
app2.jar -> log4j.properties
log4j.rootLogger=info, infoejb
log4j.appender.infoejb=org.apache.log4j.RollingFileAppender
log4j.appender.infoejb.File=/var/log/ejbinfo.log
log4j.appender.infoejb.MaxFileSize=20000KB
log4j.appender.infoejb.MaxBackupIndex=20
log4j.appender.infoejb.layout=org.apache.log4j.PatternLayout
log4j.appender.infoejb.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] [%t] %c %x - %m%n
app3.war -> log4j.properties
log4j.rootLogger=info, infoapp2
log4j.appender.infoapp2=org.apache.log4j.RollingFileAppender
log4j.appender.infoapp2.File=/var/log/infoapp2.log
log4j.appender.infoapp2.MaxFileSize=20000KB
log4j.appender.infoapp2.MaxBackupIndex=20
log4j.appender.infoapp2.layout=org.apache.log4j.PatternLayout
log4j.appender.infoapp2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] [%t] %c %x - %m%n
I call logger:
protected static final Logger LOG = Logger.getLogger(JAXSecurutyValidation.class);
The problem is that the information is recorded in only one file the ejb, the others files are not created.
How can I fix it?
SOLVED
I find the solution, is very simple update to glasfish 5 and all works fine, thanks.

Maybe this can help you:
String log4jfile = "log4 file path";
PropertyConfigurator.configure(log4jfile);
You can call each log4j file for each application.

Related

log4j, need to display only INFO and ERROR message in log file

I am new to log4j and worked to setup on eclipse and its running. I understood the chain of priority in the levels and this is my properties file config:
log4j.rootLogger=ALL, file, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/justfortesting.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.MaxFileSize=1KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
My question is
Is it possible to print only INFO and ERROR type of messages in log files with properties file configuration
Finally I got the solution for my requirement. Here I used concept of filters i.e LevelRangeFilter. And for every logging level we defined appender.
log4j.rootLogger=DEBUG, file, console, file1
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/justfortesting.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.file.filter.a.LevelMin=INFO
log4j.appender.file.filter.a.LevelMax=INFO
log4j.appender.file.MaxFileSize=1KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.appender.file1=org.apache.log4j.RollingFileAppender
log4j.appender.file1.File=logs/justfortesting.log
log4j.appender.file1.Append=true
log4j.appender.file1.ImmediateFlush=true
log4j.appender.file1.Threshold=DEBUG
log4j.appender.file1.filter.g=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.file1.filter.g.LevelMin=ERROR
log4j.appender.file1.filter.g.LevelMax=ERROR
log4j.appender.file1.MaxFileSize=1KB
log4j.appender.file1.MaxBackupIndex=2
log4j.appender.file1.layout=org.apache.log4j.PatternLayout
log4j.appender.file1.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.logger.com.log4j=DEBUG, file, console, file1
log4j.additivity.com.log4j=false
If any one suggest to reduce the above properties configuration for same requirement,, please comment.
Try
log4j.appender.file.Threshold=INFO
Also see related SO question and documentation

Tomcat Save Log History

I am using log4j in webapp deployed on tomcat, but cuurently my logs comes under catalina.log file, but only error logs remain in history, i want my all logs to be remain in history , additionally it will be good if if i get logs datewise.
my logs4j looks like this
log4j.rootLogger=INFO, myConsoleAppender
log4j.appender.myConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.myConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsoleAppender.layout.ConversionPattern= [%t] %d{dd MMM yyyy HH:mm:ss,SSS} : %c %x : %m%n
i am only using info level of logs.
Try this
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%-5p : %d{yyyy-MM-dd HH:mm:ss} %c{1}:%L - %m%n
# Define the daily rolling file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=/logs/app.log
log4j.appender.FILE.DatePattern='.'yyyy-MMM-dd
# Define the layout for the file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern= %-5p : %d{yyyy-MM-dd HH:mm:ss} %c{1}:%L - %m%n
Above will create a log file on the tomcat installation directory under the folder logson daily basis. You can change the log file path by modifying the log4j.appender.FILE.File=/path/to/logfile

log4j reading config file error

I have java web application deployed in Tomcat. I created a classes folder under WEB-INF and had log4j.properties file under it. I want the logger to be appended in a file.
log4j.properties file :
log4j.rootLogger=DEBUG, CA
# AdminFileAppender - used to log messages in the admin.log file.
log4j.appender.AdminFileAppender=org.apache.log4j.FileAppender
log4j.appender.AdminFileAppender.File=admin.log
log4j.appender.AdminFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.AdminFileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n
# ReportFileAppender - used to log messages in the report.log file.
log4j.appender.ReportFileAppender=org.apache.log4j.FileAppender
log4j.appender.ReportFileAppender.File=report.log
log4j.appender.ReportFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ReportFileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n
# ConsoleAppender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n
log4j.logger.com.sample.cfd=WARN,AdminFileAppender
log4j.logger.com.sample.cfd=DEBUG,ReportFileAppender
Code in Servlet
private static Logger logger = Logger.getLogger(CfdServlet.class);
dpPost(...){
PropertyConfigurator.configure("log4j.properties");
logger.debug("Sample debug message");
}
Error Message :
I get the following error as its not able to find my configuration file under WEB_INF/classes/log4j.properties
log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (No such file or directory)
Is something wrong in the location of my properties file.

log4j: Doesn't reflect settings in log4j.properties

I have an Java-SE app with log4j over slf4j.
At some phase of development, it stopped logging DEBUG level, and I can't figure out why.
I already excluded the causes I encountered earlier:
No WARNings
slf4j implementation used is log4j (AFAICT)
log4j.properties at right place (classpath root)
log4j.properties does end up in the .jar
The structure of the app didn't change
No log4j.properties or log4j.xml hidden in some .jar (like Weld-se-core has)
log4j.category. entries match (they didn't change since it worked)
Any ideas?
log4j.rootLogger=DEBUG, stdout, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d %-5r %-5p [%c] (%t:%x) %m%n
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p [%t] %c %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=JawaBot.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p [%t] %c %m%n
log4j.category.org = DEBUG
log4j.category.org.jboss.jawabot = DEBUG
log4j.category.org.jboss.jawabot.irc = DEBUG
log4j.category.org.mortbay.log = WARN
log4j.category.org.apache.wicket = INFO
log4j.category.org.jboss.weld = DEBUG
#log4j.category.org.jboss.weld.bootstrap = DEBUG
log4j.category.org.jboss.weld.environment.se.jpa = DEBUG

multiple fileappenders in log4j

I have to put the log info in two separate log files based on some condition.how to do that.
here is my logging.properties file
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.RollingFileAppender
log4j.appender.stdout.File=${catalina.home}/logs/std.log
log4j.appender.stdout.MaxFileSize=200KB
log4j.appender.stdout.MaxBackupIndex=2
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%c] %p - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/demo.log
log4j.appender.R.MaxFileSize=200KB
log4j.appender.R.MaxBackupIndex=2
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%c] %p - %m%n
You can define multiple appenders and assign java packages to the appenders. In the example below all classes in com.mycorp.web will log to web.log at DEBUG level and classes in com.mycorp.db will log to db.log at INFO level.
log4j.rootLogger=debug, weblogger, dblogger
#Define which packages use which appenders
log4j.logger.com.mycorp.web=DEBUG,weblogger
log4j.logger.com.mycorp.db=INFO,dblogger
#Ensure the logs don't add to each other
log4j.additivity.com.mycorp.web=false
log4j.additivity.com.mycorp.db=false
#Define web appender
log4j.appender.weblogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.weblogger.File=/var/log/myapp/web.log
log4j.appender.weblogger.DatePattern='.'yyyy-MM-dd
log4j.appender.weblogger.Append=true
log4j.appender.weblogger.layout=org.apache.log4j.PatternLayout
log4j.appender.weblogger.layout.ConversionPattern=%d [%t] %-5p %c{1} - %m %n
#Define db appender
log4j.appender.dblogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dblogger.File=/var/log/myapp/db.log
log4j.appender.dblogger.DatePattern='.'yyyy-MM-dd
log4j.appender.dblogger.Append=true
log4j.appender.dblogger.layout=org.apache.log4j.PatternLayout
log4j.appender.dblogger.layout.ConversionPattern=%d [%t] %-5p %c{1} - %m %n
Based on your condition, you can grab different loggers.
Logger x = predicate() ? Logger.getLogger("wombat") : Logger.getLogger("other");
http://logging.apache.org/log4j/1.2/manual.html (Search for "wombat" for the relevant code.)
If you want different file for some logger, you should write something like this:
log4j.logger.LOGGER_ONE=DEBUG, stdout
log4j.logger.LOGGER_TWO=WARN, R

Categories