multiple log levels in same log file in log4j - java

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.

Related

Set specific level ThresholdFilter

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?

Java log4j No appenders could be found for logger

I am trying to make a custom logger and appender in log4j, but I am geting confusing error messages.
Here's the error:
[n.a.:n.a.] 19.Apr.2016 15:54 81 [ preRegister] ERROR stderr - log4j:WARN No appenders could be found for logger (datenImportLogger).
[n.a.:n.a.] 19.Apr.2016 15:54 81 [ preRegister] ERROR stderr - log4j:WARN Please initialize the log4j system properly.
[n.a.:n.a.] 19.Apr.2016 15:54 81 [ preRegister] ERROR stderr - log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Here's my config:
# datenImportLogger
log4j.logger.datenImportLogger=datenImportFileAppender
log4j.additivity.datenImportLogger=false
log4j.appender.datenImportFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.datenImportFileAppender.File=java/log/datenimport.log
log4j.appender.datenImportFileAppender.MaxBackupIndex=5
log4j.appender.datenImportFileAppender.MaxFileSize=5MB
log4j.appender.datenImportFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.datenImportFileAppender.layout.ConversionPattern=[%X{USER_CODE}:%X{CALLER_ID}] %d{dd.MMM.yyyy HH:mm:ss,SSS} - %m%n
Have I missed something?
Are there any debug Abilities which provide debug on runtime,
because -Dlog4j.debug is not posible.
Logger should be called in code like :
private static final Logger logger = Logger.getLogger("datenImportLogger");
Root Logger is already configured.
The line
log4j.logger.datenImportLogger=datenImportFileAppender is problematic, it miss the level definition
The logger definition should be:
log4j.logger.loggerName=[level|INHERITED|NULL], [appenderName1, appenderName2,...]
source: The Complete Log4j Manual
e.g.
log4j.logger.datenImportLogger=INFO, datenImportFileAppender
try placing log4j.debug=true in top of the log4.propeties file it should provide additional logging information (although it will have affect only after the configurations are parsed )

Get org.springframework to stop polluting my logs

I am using Payara 4.1 and Netbeans 8.1.
When I run my application, these four lines are among the first to be logged:
#!## LogManagerService.postConstruct : rootFolder=/opt/payara41/glassfish
#!## LogManagerService.postConstruct : templateDir=/opt/payara41/glassfish/lib/templates
#!## LogManagerService.postConstruct : src=/opt/payara41/glassfish/lib/templates/logging.properties
#!## LogManagerService.postConstruct : dest=/opt/payara41/glassfish/domains/domain1/config/logging.properties
I added the line org.springframework=WARNING at the end of the last logging.properties file given above, and restarted my server. That didn't seem to have an effect. When I open the asadmin shell /opt/payara41/bin/asadmin and run list-log-attributes, here is what I get:
asadmin> list-log-attributes
com.sun.enterprise.server.logging.GFFileHandler.excludeFields <>
com.sun.enterprise.server.logging.GFFileHandler.file <${com.sun.aas.instanceRoot}/logs/server.log>
com.sun.enterprise.server.logging.GFFileHandler.flushFrequency <1>
com.sun.enterprise.server.logging.GFFileHandler.formatter <com.sun.enterprise.server.logging.ODLLogFormatter>
com.sun.enterprise.server.logging.GFFileHandler.logtoConsole <false>
com.sun.enterprise.server.logging.GFFileHandler.maxHistoryFiles <0>
com.sun.enterprise.server.logging.GFFileHandler.multiLineMode <true>
com.sun.enterprise.server.logging.GFFileHandler.retainErrorsStasticsForHours <0>
com.sun.enterprise.server.logging.GFFileHandler.rotationLimitInBytes <2000000>
com.sun.enterprise.server.logging.GFFileHandler.rotationOnDateChange <false>
com.sun.enterprise.server.logging.GFFileHandler.rotationTimelimitInMinutes <0>
com.sun.enterprise.server.logging.SyslogHandler.useSystemLogging <false>
handlerServices <com.sun.enterprise.server.logging.GFFileHandler,com.sun.enterprise.server.logging.SyslogHandler>
handlers <java.util.logging.ConsoleHandler>
java.util.logging.ConsoleHandler.formatter <com.sun.enterprise.server.logging.UniformLogFormatter>
java.util.logging.FileHandler.count <1>
java.util.logging.FileHandler.formatter <java.util.logging.XMLFormatter>
java.util.logging.FileHandler.limit <50000>
java.util.logging.FileHandler.pattern <%h/java%u.log>
log4j.logger.org.hibernate.validator.util.Version <warn>
org.springframework <WARNING>
Command list-log-attributes executed successfully.
I have tried the suggestions given in this SO question but that didn't work. I'm stumped. :( I really do not want to see all those INFO logging lines printed by the spring framework.
=== EDIT ===
Here is what is in my log4j.properties (copied from SO):
# Define the root logger with appender file
log4j.rootLogger = DEBUG, stdout
# Define the file appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# Set the name of the logs destination
log4j.appender.stdout.target=System.out
# Set the immediate flush to true (default)
log4j.appender.stdout.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.stdout.Threshold=debug
# Set the append to false, overwrite
log4j.appender.stdout.Append=false
# Define the layout for appender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.conversionPattern=%d{yyyy-MM-dd}:%m%n
log4j.logger.org.springframework=WARNING
Here are all the places I have copied that file to:
$PROJECT_DIR/src/log4j.properties
$PROJECT_DIR/src/main/resources/META-INF/log4j.properties
$PROJECT_DIR/src/main/webapp/WEB-INF/log4j.properties
$PROJECT_DIR/src/main/webapp/WEB-INF/classes/log4j.properties
$PROJECT_DIR/target/$PROJECT-1.0/WEB-INF/log4j.properties
$PROJECT_DIR/target/$PROJECT-1.0/WEB-INF/classes/log4j.properties
$PROJECT_DIR/target/$PROJECT-1.0/WEB-INF/classes/META-INF/log4j.properties
$PROJECT_DIR/target/classes/META-INF/log4j.properties
/opt/payara41/glassfish/domains/domain1/config/log4j.properties
In addition, I have added log4j.logger.org.springframework=WARNING and org.springframework=WARNING to Configurations > server-config > Logger Settings > Module Log Levels in the Payara UI. Nothing is working. Payara's server log still has INFO lines for the spring framework.
I tried everything that has been suggested so far, to no avail. So I ended up doing something pretty hacky. I wrote my own log handler and installed it to domain1/lib/ext:
package org.springsuppressor;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
public class MyHandler extends ConsoleHandler {
public MyHandler() {
super();
setLevel(Level.WARNING);
}
}
The handler above sets the log level for all loggers to WARNING. I could suppress messages for just the spring framework, but I don't yet need to do that.

how to disable a log4j timestamp for only particular lines

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");

Log Level per appender for a single Logger

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.

Categories