log4j - Why doesn't my appender show message? - java

At my work, I have inherited of the maintenance of a satandalone application.
The following code configures Log4J but no messages can be seen on the console.
LogManager.resetConfiguration();
PatternLayout layout = new PatternLayout();
layout.setConversionPattern("RECORD-BACKEND / (%-5p) %m (%F:%L)%n");
ConsoleAppender stderr = new ConsoleAppender();
stderr.setTarget(ConsoleAppender.SYSTEM_ERR);
stderr.setLayout(layout);
stderr.addFilter(new CurrentThreadLogFilter());
stderr.setThreshold(Level.INFO);
stderr.activateOptions();
Logger loggerRECORD = getLoggerRECORD();
loggerRECORD.setAdditivity(false);
loggerRECORD.addAppender(stderr);
Logger root = Logger.getRootLogger();
root.setLevel(Level.WARN);
root.addAppender(stderr);
// some lines forward ...
loggerRECORD.info("Process starting...");
What am I missing ?

Unfortunately you have not sent the code that actually prints the message. However I can assume that you try to do something like this:
logger.info("my message");
In this case your message will not be printed because it is filtered out by root logger defined to print warnings.
Loggers in Log4J are stored as hierarchical structure. The root logger is the entry point to this tree. Each logger filters log messages according to configured level. Therefore if upper level logger (root in your case) filters log message it even does not arrive to lower level logger and definitely does not arrive to appender.
The solution for you is to define root logger to allow ALL messages.
And the last note: do you have any special reasons to configure logger programmatically? Log4J can be configured using properties or (better) xml file. Take a look on this document.
And yet another note. Log4J has been deprecated by its creator. If you are starting now go forward to Logback as a logger and SLF4J as a light weight log interface.

Thanks to AlexR, here how I solved my problem:
LogManager.resetConfiguration();
PatternLayout layout = new PatternLayout();
layout.setConversionPattern("RECORD-BACKEND / (%-5p) %m (%F:%L)%n");
ConsoleAppender stderr = new ConsoleAppender();
stderr.setTarget(ConsoleAppender.SYSTEM_ERR);
stderr.setLayout(layout);
stderr.addFilter(new CurrentThreadLogFilter());
stderr.setThreshold(Level.INFO);
stderr.activateOptions();
Logger loggerRECORD = getLoggerRECORD();
loggerRECORD.setLevel( /* get Log Level from env. */ );
loggerRECORD.setAdditivity(false);
loggerRECORD.addAppender(stderr);
Logger root = Logger.getRootLogger();
root.setLevel(Level.WARN);
root.addAppender(stderr);
// some lines forward ...
loggerRECORD.info("Process starting...");
Since additivity is set to false for loggerRECORD, the appender sucessfully print any message from INFO to ERROR.

Related

Log4j selective appender

I have a java application which has 'hypotetically speaking' 3 objects...
1 of the class Animal, 1 of the class Food, those are not related by any inheritance or interface..and a last one of the class Manager wich is having a list of animals and list of Food, the manager is responsable for a zoo where those animals and Food are..
to the point...
Am using log4j and I need to log to the a txt file IF ONLY AND ONLY IF something in the animal list changes... (animal dies, borns or what ever...) and I need to log to the System.out IF AND ONLY IF something in the Food list changes... (new food is need, food was eaten, what ever...)
My question:
How can I do that with log4j?
I have found here:
Log4j : Creating/Modifying appenders at runtime, log file recreated and not appended
something like dynamically changing the appender
String targetLog="where ever you want your log"
FileAppender apndr = new FileAppender(new PatternLayout("%d %-5p [%c{1}] %m%n"),targetLog,true);
logger.addAppender(apndr);
logger.setLevel((Level) Level.ALL);
but I think this is very ugly and error prone to add and remove the appender constantly all over the hole application..
Is there any better way to handle this
can I have 2 logger (one for animals 1 for the food)??
any suggestion??
Thanks
You can do this strictly from the configuration in the log4j.xml file. You can define two appenders in there, and then have two logger elements, one that ties animals to the first appender, and one that ties the food to another appender. Probably you should have a element too, to define default behavior.
This is how I got it to work:
Configuring the properties
log4j.rootLogger=TRACE, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%5F:%t:%L] - %m%n
log4j.appender.animalLogger=org.apache.log4j.FileAppender
log4j.appender.animalLogger.File=animal.log
log4j.appender.animalLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.animalLogger.layout.ConversionPattern=%d [%5F:%t:%L] - %m%n
log4j.category.animalLogger=DEBUG, animalLogger
log4j.additivity.animalLogger=false
log4j.category.foodlLogger=DEBUG, stdout
log4j.additivity.foodlLogger=false
static final Logger animalLogger = Logger.getLogger("animalLogger");
static final Logger foodlLogger = Logger.getLogger("foodlLogger");
and to load the logger and logging:
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
animalLogger.debug("Hello animalLogger message");
foodlLogger.debug("Hello reportsLog message");
}

Dropwizard custom logger does not dump logs into the file

I need to set maxfile size for my application but currently i am using Dropwizard core version 0.8.4 and its file appender does not support this feature.
So I approached like below by writing a custom appender as updating to latest dropwizard(which support my need) version not an option right now.
private void initLogging(Configuration configuration) throws JoranException {
final File logDir = new File("/tmp/enforcer");
final File logFile = new File(logDir, "wallet.log");
final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
rollingFileAppender.setFile(logFile.getAbsolutePath());
rollingFileAppender.setName("com.documents4j.logger.server.file");
rollingFileAppender.setContext(loggerContext);
FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy();
fixedWindowRollingPolicy.setFileNamePattern(logFile.getAbsolutePath() +"%i.gz");
fixedWindowRollingPolicy.setMaxIndex(7);
fixedWindowRollingPolicy.setContext(loggerContext);
fixedWindowRollingPolicy.setParent(rollingFileAppender);
fixedWindowRollingPolicy.start();
SizeBasedTriggeringPolicy<ILoggingEvent> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
sizeBasedTriggeringPolicy.setMaxFileSize("2KB");
sizeBasedTriggeringPolicy.setContext(loggerContext);
sizeBasedTriggeringPolicy.start();
rollingFileAppender.setRollingPolicy(fixedWindowRollingPolicy);
rollingFileAppender.setTriggeringPolicy(sizeBasedTriggeringPolicy);
rollingFileAppender.start();
System.out.println("Logging: The log is written to " + logFile);
final ch.qos.logback.classic.Logger log = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
log.setLevel(Level.DEBUG);
log.addAppender(rollingFileAppender);
}
#Override
public void run(Configuration configuration, Environment environment) throws Exception
{
initLogging(configuration);
}
My yaml file config is
logging:
level: INFO
org.springframework.retry.support.RetryTemplate: DEBUG
appenders:
- type: file
currentLogFilename: /tmp/enforcer.log
threshold: ALL
archive: true
archivedLogFilenamePattern: /tmp/enforcer-%d.log
archivedFileCount: 5
timeZone: UTC
logFormat: '%-5level [%date{yyyy-MM-dd HH:mm:ss,SSS}] [%X{realdocRequestId}] %logger{15}: %m%n'
Now when i run my application i noticed, even though custom log file (/tmp/enforcer/wallet.log) is created in the particular directory but actual log is not dumped i.e. wallet.log file size is 0 kb where as the log file configured in yaml is created and size is certain kb and goes on increasing as log event is generated.
I am not able figure out what is wrong im doing, help will be appreciated.
your logger doesn't log anything because you never set an encoder to it. Set a breakpoint in:
OutputStreamAppender#start() and you will see that there is no encoder.
It will work once you add:
LayoutWrappingEncoder<ILoggingEvent> layoutEncoder = new LayoutWrappingEncoder<>();
DropwizardLayout formatter = new DropwizardLayout(loggerContext, TimeZone.getDefault());
formatter.setPattern("[%level] [%h] %d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z', UTC} [%thread] [%logger] %msg%n");
layoutEncoder.setLayout(formatter);
formatter.start();
rollingFileAppender.setEncoder(layoutEncoder);
Of course you can define whichever format you like (and formatter).
But keep in mind that this is a fairly hacky example of what you try to achieve. You now have 2 points in code where you configure logging (DW and your own). You have yaml configured logging as well as your own. I recommend investing a bit of work and adding a proper AppenderFactory that you can use.
Hope that helps,
Artur

How to log different loggers into different files

Here is what i have in my java code
private static final Logger SUCCESS = LogManager.getLogger("success");
private static final Logger ERROR = LogManager.getLogger("error");
And I need to create 2 log files for these logs. (i tried to follow Creating multiple log files of different content with log4j) but I think its not exactly the same. But here goes what i created in log4j.properties.
log4j.rootLogger=TRACE, SuccessAppender, ErrorAppender
# setup SuccessAppender
log4j.appender.SuccessAppender=org.apache.log4j.RollingFileAppender
log4j.appender.SuccessAppender.Threshold=INFO
log4j.appender.SuccessAppender.File=success.log
log4j.appender.SuccessAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.SuccessAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# setup ErrorAppender
log4j.appender.ErrorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.ErrorAppender.Threshold=INFO
log4j.appender.ErrorAppender.File=error.log
log4j.appender.ErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
I was wondering how to map this "success" log into the "SuccessAppender".
First of all you have to make sure that the RootLogger is not logging anything. This can be achieved in different ways but my first solution is to let it append to the NullAppender.
log4j.rootLogger=OFF, NullAppender
# setup NullAppender
log4j.appender.NullAppender=org.apache.log4j.varia.NullAppender
Then you'll have to set the log level and the appender for your loggers (success and error):
log4j.logger.success = TRACE, SuccessAppender
log4j.logger.error = TRACE, ErrorAppender
And the rest of your log4j.properties is left as is.
An alternate solution if you don't want to use the NullAppender would be to set the additivity flag on the loggers:
log4j.rootLogger=OFF, SuccessAppender, ErrorAppender
log4j.logger.success = TRACE, SuccessAppender
log4j.additivity.success = false
log4j.logger.error = TRACE, ErrorAppender
log4j.additivity.error = false
Some more information about the additivity can be found in the Log4J manual.
Appender Additivity
The output of a log statement of logger C will go to all the appenders in C and its ancestors. This is the meaning of the term "appender additivity".
However, if an ancestor of logger C, say P, has the additivity flag set to false, then C's output will be directed to all the appenders in C and its ancestors upto and including P but not the appenders in any of the ancestors of P.
Loggers have their additivity flag set to true by default.
try
log4j.logger.success=TRACE, SuccessAppender

LogBack RollingFileAppender Not Writing Log File (Though FileAppender works)

I'm trying to programmatically configure LogBack's RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender) and it doesn't seem to be working. When I'm using FileAppender, everything seems to be working fine with exact same configuration (less policies/trigger) so I'm guessing it's not a permission issue. I tried commenting out all policy configuration and that didn't help either. Below is my sample code, with some hard-coded values. Also, there's no error at all what so ever. When I debug the LogBack source code, I didn't see anything that could have gone wrong.
Any hint is appreciative. I need to get this working without a configuration file since that's a restriction in my organization. I'm testing this out on a MacBook.
Logger logger = (Logger)LoggerFactory.getLogger(applicationName);
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
lc.reset();
RollingFileAppender<ILoggingEvent> fileAppender =
new RollingFileAppender<ILoggingEvent>();
fileAppender.setAppend(true);
fileAppender.setFile("/Users/Jack/Desktop/logs/" + applicationName + ".log");
fileAppender.setContext(lc);
SizeBasedTriggeringPolicy<ILoggingEvent> rPolicy =
new SizeBasedTriggeringPolicy<ILoggingEvent>("20MB");
fileAppender.setTriggeringPolicy(rPolicy);
TimeBasedRollingPolicy<ILoggingEvent> tPolicy =
new TimeBasedRollingPolicy<ILoggingEvent>();
tPolicy.setFileNamePattern("/archive/" + applicationName + ".%d");
tPolicy.setMaxHistory(180);
tPolicy.setParent(fileAppender);
tPolicy.setContext(lc);
PatternLayout pl = new PatternLayout();
pl.setPattern("%d %5p %t [%c:%L] %m%n)");
pl.setContext(lc);
pl.start();
fileAppender.setLayout(pl);
fileAppender.start();
logger.addAppender(fileAppender);
logger.setLevel(Level.DEBUG);
logger.debug("Test message");
The key issues are as follow:
RollingFileAppender must have RollingPolicy
RollingFileAppender requires PatternLayoutEncoder instead of PatternEncoder
RollingPolicy must also be started or certain properties will be null
What made this very difficult to figure out is that I couldn't figure out how to make BasicStatusManager print out error message. I ended up having to use the following code to print everything out.
for(Status status : logger.getLoggerContext().getStatusManager().getCopyOfStatusList()){
System.out.println(status.getOrigin() + " - " + status.getMessage());
}
There is a separate thread going on as mentioned in the comment above on why LogBack log messages are not printing out. I also have an email thread going on with Nabble. Will post the solution in that thread as soon as I or someone can figure this out.

Why are the Level.FINE logging messages not showing?

The JavaDocs for java.util.logging.Level state:
The levels in descending order are:
SEVERE (highest value)
WARNING
INFO
CONFIG
FINE
FINER
FINEST (lowest value)
Source
import java.util.logging.*;
class LoggingLevelsBlunder {
public static void main(String[] args) {
Logger logger = Logger.getAnonymousLogger();
logger.setLevel(Level.FINER);
System.out.println("Logging level is: " + logger.getLevel());
for (int ii=0; ii<3; ii++) {
logger.log(Level.FINE, ii + " " + (ii*ii));
logger.log(Level.INFO, ii + " " + (ii*ii));
}
}
}
Output
Logging level is: FINER
Jun 11, 2011 9:39:23 PM LoggingLevelsBlunder main
INFO: 0 0
Jun 11, 2011 9:39:24 PM LoggingLevelsBlunder main
INFO: 1 1
Jun 11, 2011 9:39:24 PM LoggingLevelsBlunder main
INFO: 2 4
Press any key to continue . . .
Problem statement
My example sets the Level to FINER, so I was expecting to see 2 messages for each loop. Instead I see a single message for each loop (the Level.FINE messages are missing).
Question
What needs changing in order to see the FINE (, FINER or FINEST) output?
Update (solution)
Thanks to Vineet Reynolds' answer, this version works according to my expectation. It displays 3 x INFO messages, & 3 x FINE messages.
import java.util.logging.*;
class LoggingLevelsBlunder {
public static void main(String[] args) {
Logger logger = Logger.getAnonymousLogger();
// LOG this level to the log
logger.setLevel(Level.FINER);
ConsoleHandler handler = new ConsoleHandler();
// PUBLISH this level
handler.setLevel(Level.FINER);
logger.addHandler(handler);
System.out.println("Logging level is: " + logger.getLevel());
for (int ii=0; ii<3; ii++) {
logger.log(Level.FINE, ii + " " + (ii*ii));
logger.log(Level.INFO, ii + " " + (ii*ii));
}
}
}
Loggers only log the message, i.e. they create the log records (or logging requests). They do not publish the messages to the destinations, which is taken care of by the Handlers. Setting the level of a logger, only causes it to create log records matching that level or higher.
You might be using a ConsoleHandler (I couldn't infer where your output is System.err or a file, but I would assume that it is the former), which defaults to publishing log records of the level Level.INFO. You will have to configure this handler, to publish log records of level Level.FINER and higher, for the desired outcome.
I would recommend reading the Java Logging Overview guide, in order to understand the underlying design. The guide covers the difference between the concept of a Logger and a Handler.
Editing the handler level
1. Using the Configuration file
The java.util.logging properties file (by default, this is the logging.properties file in JRE_HOME/lib) can be modified to change the default level of the ConsoleHandler:
java.util.logging.ConsoleHandler.level = FINER
2. Creating handlers at runtime
This is not recommended, for it would result in overriding the global configuration. Using this throughout your code base will result in a possibly unmanageable logger configuration.
Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.FINER);
Logger.getAnonymousLogger().addHandler(consoleHandler);
The Why
java.util.logging has a root logger that defaults to Level.INFO, and a ConsoleHandler attached to it that also defaults to Level.INFO.
FINE is lower than INFO, so fine messages are not displayed by default.
Solution 1
Create a logger for your whole application, e.g. from your package name or use Logger.getGlobal(), and hook your own ConsoleLogger to it.
Then either ask root logger to shut up (to avoid duplicate output of higher level messages), or ask your logger to not forward logs to root.
public static final Logger applog = Logger.getGlobal();
...
// Create and set handler
Handler systemOut = new ConsoleHandler();
systemOut.setLevel( Level.ALL );
applog.addHandler( systemOut );
applog.setLevel( Level.ALL );
// Prevent logs from processed by default Console handler.
applog.setUseParentHandlers( false ); // Solution 1
Logger.getLogger("").setLevel( Level.OFF ); // Solution 2
Solution 2
Alternatively, you may lower the root logger's bar.
You can set them by code:
Logger rootLog = Logger.getLogger("");
rootLog.setLevel( Level.FINE );
rootLog.getHandlers()[0].setLevel( Level.FINE ); // Default console handler
Or with logging configuration file, if you are using it:
.level = FINE
java.util.logging.ConsoleHandler.level = FINE
By lowering the global level, you may start seeing messages from core libraries, such as from some Swing or JavaFX components.
In this case you may set a Filter on the root logger to filter out messages not from your program.
WHY
As mentioned by #Sheepy, the reason why it doesn't work is that java.util.logging.Logger has a root logger that defaults to Level.INFO, and the ConsoleHandler attached to that root logger also defaults to Level.INFO. Therefore, in order to see the FINE (, FINER or FINEST) output, you need to set the default value of the root logger and its ConsoleHandler to Level.FINE as follows:
Logger.getLogger("").setLevel(Level.FINE);
Logger.getLogger("").getHandlers()[0].setLevel(Level.FINE);
The problem of your Update (solution)
As mentioned by #mins, you will have the messages printed twice on the console for INFO and above: first by the anonymous logger, then by its parent, the root logger which also has a ConsoleHandler set to INFO by default. To disable the root logger, you need to add this line of code: logger.setUseParentHandlers(false);
There are other ways to prevent logs from being processed by default Console handler of the root logger mentioned by #Sheepy, e.g.:
Logger.getLogger("").getHandlers()[0].setLevel( Level.OFF );
But Logger.getLogger("").setLevel( Level.OFF ); won't work because it only blocks the message passed directly to the root logger, not the message comes from a child logger. To illustrate how the Logger Hierarchy works, I draw the following diagram:
public void setLevel(Level newLevel) set the log level specifying which message levels will be logged by this logger. Message levels lower than this value will be discarded. The level value Level.OFF can be used to turn off logging. If the new level is null, it means that this node should inherit its level from its nearest ancestor with a specific (non-null) level value.
why is my java logging not working
provides a jar file that will help you work out why your logging in not working as expected.
It gives you a complete dump of what loggers and handlers have been installed and what levels are set and at which level in the logging hierarchy.
Tried other variants, this can be proper
Logger logger = Logger.getLogger(MyClass.class.getName());
Level level = Level.ALL;
for(Handler h : java.util.logging.Logger.getLogger("").getHandlers())
h.setLevel(level);
logger.setLevel(level);
// this must be shown
logger.fine("fine");
logger.info("info");
I found my actual problem and it was not mentioned in any answer: some of my unit-tests were causing logging initialization code to be run multiple times within the same test suite, messing up the logging on the later tests.
This solution appears better to me, regarding maintainability and design for change:
Create the logging property file embedding it in the resource project folder, to be included in the jar file:
# Logging
handlers = java.util.logging.ConsoleHandler
.level = ALL
# Console Logging
java.util.logging.ConsoleHandler.level = ALL
Load the property file from code:
public static java.net.URL retrieveURLOfJarResource(String resourceName) {
return Thread.currentThread().getContextClassLoader().getResource(resourceName);
}
public synchronized void initializeLogger() {
try (InputStream is = retrieveURLOfJarResource("logging.properties").openStream()) {
LogManager.getLogManager().readConfiguration(is);
} catch (IOException e) {
// ...
}
}
To change the logcat level:
adb shell setprop log.tag.<YOUR_LOG_TAG> <LEVEL>
For example:
C:\Users\my_name\AppData\Local\Android\Sdk\platform-tools\adb.exe shell setprop log.tag.com.mycompany.myapp VERBOSE
Here is why this works for Logger:
The root logger, which is the parent of your logger, is using an internal logging handler com.android.internal.logging.AndroidHandler. This handler in its publish method is checking Log.isLoggable and writing to Log.
Note about <YOUR_LOG_TAG>:
Basically this is the name of your Logger. For global Logger it is Logger.GLOBAL_LOGGER_NAME. For anonymous logger it is "null". Tag is limited to 23 characters.
AndroidHandler does a transformation from the logger name to the tag:
private static String loggerNameToTag(String loggerName) {
// Anonymous logger.
if (loggerName == null) {
return "null";
}
int length = loggerName.length();
if (length <= 23) {
return loggerName;
}
int lastPeriod = loggerName.lastIndexOf(".");
return length - (lastPeriod + 1) <= 23
? loggerName.substring(lastPeriod + 1)
: loggerName.substring(loggerName.length() - 23);
}

Categories

Resources