I recently had to find a way to modify the log level of different packages on the fly. This post is more of a reminder for my future self, but hopefully it’ll help others too.
This is obviously an oversimplified example. Feel free to add it in a package names loop that could be populated from a database or other services.
$.SyntaxHighlighter.init();
// Retrieve the logger based on it’s name
ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger("com.andrei.foo.bar");
// Set it’s new level
logger.setLevel(Level.ERROR);
Logging is one of the fundamental concepts of software development. It helps developers to solve different types of problems by keeping track of information that is processed during the execution of a program. ABAP development has its own tracing and logging features which are not used so frequently compared with other development environments; however when it comes to java applications, logging…
Using YAML in Spring Boot to Configure Logback #Springboot #springframework
When it comes to logging in enterprise applications, logback makes an excellent choice – it’s simple and fast, has powerful configuration options, and comes with a small memory footprint. I have introduced logback in my introductory post, Logback Introduction: An Enterprise Logging Framework. YAML is just one option you can use for Spring Boot configuration. In a series of posts on logback, I’ve…
Logging is a very common use case in almost any kind of scenario. Starting from writing System.out.println("Hello World!"); to logger.error("Error in Db connection". e.getThrowable());
The most basic need of logging is for us to understand the code execution better, to know the value of a variable when a line is executed and to be able to debug issues in production code without stopping the server.
Logging doesn’t seem like an important thing till we face issues in production code, Logs are the first point of analysis and better logging would enable quicker troubleshooting.
How you log errors is important and this depends on your exception handling strategy. For J2EE layered application, the strategy could be applied different for each layer or globally through Filters / Interceptors.
This post is not an in depth description of logging or log4j or sl4j but a gist of the various components and aspects involved in logging. This may help as a refresher to some one who uses logging. For a detail tutorial on logging check this http://tutorials.jenkov.com/java-logging/index.html
There are various frameworks in java to help us with logging some of which are (Note there are many more I am just listing the ones I have used):
1) log4j
2) java.util.logging
I guess log4j is more popular choice than the other.
The basics of Logging can be divided into the following components:
Loggers need to be initialised at the start of code execution, so that its settings are propagated through out the rest of the code. If you initialise a logger after some code is executed. That code would be logged differently based on its api default configurations.
To put this is in simple terms..
For a standalone simple Java Application Logger has to be initialised at the first line of public static void main(String[] args)
For J2EE Web application running on Containers (tomcat/jetty) Logger needs to be setup during server context start using the Listeners configured in web.xml. Incase of Spring based WebApp..
The above configuration ensures that log4j settings are configured first.
1) Configure different levels for different packages (root package prefix)
Very useful when you want to run your rootLogger in TRACE but you want to avoid INFO, TRACE logs from dependent jars in your project eg:
log4j.rootLogger=INFO, fileLogger
#set loggers level to WARN for Dependent frameworks/API to avoid unnecessary logging in PROD
log4j.logger.org.springframework=WARN
log4j.logger.org.apache.zookeeper=WARN
log4j.logger.com.jolbox.bonecp=WARN
log4j.logger.org.hibernate=WARN
2) Configure different appenders on different levels
Very useful if you want to email critical logs to alert techops and keep the rest INFO logs redirected to a File. Check this link for the same: http://theholyjava.wordpress.com/2011/10/15/aggregating-error-logs-to-send-a-warning-email-when-too-many-of-them-log4j-stat4j-smtpappender/
3) Configure different Layout to different appenders
Haven’t found any major use case for this. I usually prefer to keep the same layout for all my appenders.
The above code starts a daemon thread , using builtin watchdog timer to reload log4j configurations from the file very one minute.
Slf4j is not a logging framework.. It is a wrapper over the logging frameworks. Slf4j is a Facade (Facade Design pattern: http://javapapers.com/design-patterns/facade-design-pattern/). What slf4j offers is ability of API developers to allow the client of their API to decide the underlying logging framework they want to use rather than force them to use what the API designer wants.
To put that in more simpler terms, If your building a reusable API that will server as jar to many other projects, it makes more sense to to slf4j wrapper and write all your logging statements on its classes rather than use classes from log4j. This allows the guys using your jar to switch choose their logging (log4j or java.util.Logging) and apply that to the log statements of your jar.
One of the issues with logging is string concatenation of message string that is sent as argument the log method. This results in performance loss eg:
Logger.debug(“Insider class A method b variable value “+val1+”_”+val2+”_”+val3)
It gets worse when your running you code with logger level set to ERROR, as even if your not going to be loggin this message string, the concatenation still occurs.
Slf4j also offers some performance improvement methods
logger.debug(“ Inside class A method b variable value {}_{}_{}”,val1,val2,val3);
Note: here concatenation never occurs, template substitution is in play and its only executed when the appropriate level is set in the log4j.properties file.
My SLF4J logging doesn't work with JBoss 7.1 Final
I just upgraded to JBoss 7.1 final because of some random CORBA errors that were coming up trying to use Pelops with Cassandra in 7.0.1 and 7.0.2. While those errors were resolved, I could no longer see any logs. I'm not going into the details as to why this is, but simply giving you the solution that worked for me because, if you're like me, you need your fucking logs especially when deadlines are close and it's getting late.
In your Maven POM make sure you have
If you had scope set to compile, change it to provided
If you had a log4j binding like the one below, remove it
Now you also need to add some damn logging configuration to your WAR or EAR
Place this in your META-INF as jboss-deployment-structure.xml
Oh! But wait! That's not all!
You also need to add some crap to standalone.xml or standalone-full.xml (whichever server configuration you are using). So head on over to $JBOSS_HOME/standalone/configuration and open up standalone-full.xml or whatever configuration you are using and find the logging section. You'll see some loggers config. Add this:
Ok... now go restart your stuff and logging should work.
I'm sufficiently pissed that this happened and I wasted hours of my life figuring this crap out. After I click submit on this here post, I'm moving my shit to Tomcat for good. This is just way too much trouble just to get logging to work.