Hi All,
This is the starting post of logger and i will start from java logger. The idea of this post is to explain end to end flow of logging. Please keep this in mind that this is very basic post and perhaps not be useful of advance user.
Before starting to write this post i did search on google , youtube and other websites but was not able to find any very straight forward explanation.
If you are new to logging or want to refresh your brain then please start reading below document first.
Java Logger
Before starting , below classes/interfaces are part of java logger framework. And also there is one property file which being used by java logger framework.I Will go one by one as and when i am using them.
Property File : logging.proerties and this can be found in your jre location under lib folder. You can find sample property file at Logging.properties
The scope of this post are below.
1-How to create / get logger object.
2-Printing some method values which are available by default object.
3-Log level
The approach here is to start with coding instead of theory and we will understand theory as and when we needed.
you can check out code from this location. JavaLogFirst.java
1-How to create / get logger object : Creating the logger object is very straight forward. You need to just write below line of code and logger object is ready to use.
It is very easy to get logger , and if you see it is calling getLogger static method on Logger class by passing name for this logger. Below information can be found on Logger API document.
"A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.swing. In addition it is possible to create "anonymous" Loggers that are not stored in the Logger namespace."
2-Printing some method values which are available by default object : There are below methods which come with Logger class and these method are important therefore we need to understand this as well. Logger object is not simple java object because it use property file to create this object therefore it is little bit different. Hence below methods are also impact if we made any changes in this property file.
a-getName
b-getParent
c-getLevel
d-getHandlers
e-getGlobal
f-getFilter
g-getResourcebundleName
All the above method are self explainable. Please see the below comment for their output. Here i am not going to explain why some of the values are coming null. We will get to them later point. Our goal is make this very simple and straight.
This is the starting post of logger and i will start from java logger. The idea of this post is to explain end to end flow of logging. Please keep this in mind that this is very basic post and perhaps not be useful of advance user.
Before starting to write this post i did search on google , youtube and other websites but was not able to find any very straight forward explanation.
If you are new to logging or want to refresh your brain then please start reading below document first.
Java Logger
Before starting , below classes/interfaces are part of java logger framework. And also there is one property file which being used by java logger framework.I Will go one by one as and when i am using them.
S.No. | Class or Interface |
1 | Filter |
2 | LoggingMXBean |
3 | ConsoleHandler |
4 | ErrorManager |
5 | FileHandler |
6 | Formatter |
7 | Handler |
8 | Level |
9 | Logger |
10 | LoggingPermission |
11 | LogManager |
12 | LogRecord |
13 | MemoryHandler |
14 | SimpleFormatter |
15 | SocketHandler |
16 | StreamHandler |
17 | XMLFormatter |
Property File : logging.proerties and this can be found in your jre location under lib folder. You can find sample property file at Logging.properties
The scope of this post are below.
1-How to create / get logger object.
2-Printing some method values which are available by default object.
3-Log level
The approach here is to start with coding instead of theory and we will understand theory as and when we needed.
you can check out code from this location. JavaLogFirst.java
1-How to create / get logger object : Creating the logger object is very straight forward. You need to just write below line of code and logger object is ready to use.
private static Logger _alogger = Logger.getLogger( JavaLogFirst.class.getName() );
It is very easy to get logger , and if you see it is calling getLogger static method on Logger class by passing name for this logger. Below information can be found on Logger API document.
"A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.swing. In addition it is possible to create "anonymous" Loggers that are not stored in the Logger namespace."
2-Printing some method values which are available by default object : There are below methods which come with Logger class and these method are important therefore we need to understand this as well. Logger object is not simple java object because it use property file to create this object therefore it is little bit different. Hence below methods are also impact if we made any changes in this property file.
a-getName
b-getParent
c-getLevel
d-getHandlers
e-getGlobal
f-getFilter
g-getResourcebundleName
All the above method are self explainable. Please see the below comment for their output. Here i am not going to explain why some of the values are coming null. We will get to them later point. Our goal is make this very simple and straight.
/**
* This method will print default logging info.
* Logger name --> whatever we passed inside get logger will return this.
* Parent logger name --> It is returning instance of java.util.logging.LogManager$RootLogger
* Default handlers count --> 0
* Global logger --> instance of java.util.logging.Logger
* Filter Object --> null
* Log level --> null
* Since default lagger level is null therefore it is printing only warning , info and severe logs
* Its printed below messages on console
* Apr 03, 2018 7:17:53 AM view.java.jol.JavaLogOne printDefaultLoggingInfo
* WARNING: Printing logger object name view.java.jol.JavaLogOne
* Apr 03, 2018 7:17:53 AM view.java.jol.JavaLogOne printDefaultLoggingInfo
* INFO: Printing parent logger object name java.util.logging.LogManager$RootLogger@5c18298f
* Apr 03, 2018 7:17:53 AM view.java.jol.JavaLogOne printDefaultLoggingInfo
* SEVERE: Printing level class null
*/
private void printDefaultLoggingInfo()
{
_alogger.warning( "Printing logger object name " + _alogger.getName() );
_alogger.info( "Printing parent logger object name " + _alogger.getParent() );
_alogger.severe( "Printing level class " + _alogger.getLevel() );
}
* This method will print default logging info.
* Logger name --> whatever we passed inside get logger will return this.
* Parent logger name --> It is returning instance of java.util.logging.LogManager$RootLogger
* Default handlers count --> 0
* Global logger --> instance of java.util.logging.Logger
* Filter Object --> null
* Log level --> null
* Since default lagger level is null therefore it is printing only warning , info and severe logs
* Its printed below messages on console
* Apr 03, 2018 7:17:53 AM view.java.jol.JavaLogOne printDefaultLoggingInfo
* WARNING: Printing logger object name view.java.jol.JavaLogOne
* Apr 03, 2018 7:17:53 AM view.java.jol.JavaLogOne printDefaultLoggingInfo
* INFO: Printing parent logger object name java.util.logging.LogManager$RootLogger@5c18298f
* Apr 03, 2018 7:17:53 AM view.java.jol.JavaLogOne printDefaultLoggingInfo
* SEVERE: Printing level class null
*/
private void printDefaultLoggingInfo()
{
_alogger.warning( "Printing logger object name " + _alogger.getName() );
_alogger.info( "Printing parent logger object name " + _alogger.getParent() );
_alogger.severe( "Printing level class " + _alogger.getLevel() );
}
3-Log level : This is also very important but this is very straight forward. However one point to remember that they is relationship or dependency among log levels. If you pay close attention you observe that the int values are increasing from top to bottom, believe me this is not coincidence. It is done by intention. So if the log level is set to INFO then it will log all the log levels which are above its value. So in this case it will print SEVERE , WARNING and INFO log levels log.
S.No. | Level | Int Value |
1 | OFF | Integer.MAX_VALUE |
2 | SEVERE | 1000 |
3 | WARNING | 900 |
4 | INFO | 800 |
5 | CONFIG | 700 |
6 | FINE | 500 |
7 | FINER | 400 |
8 | FINEST | 300 |
9 | ALL | Integer.MIN_VALUE |
Before wrapping up this post some very important points which i would like to mention and they are
1-Default log level is NULL.
2-Default logger object only will print log message which are more than INFO. (warning and severe ). Why this ? It is because of logging.properties files. If you see this file you need to understand two points here.
a-.level= INFO
b-java.util.logging.ConsoleHandler.level = INFO
I will go this in details in next upcoming posts.
3-Default log handle class is java.util.logging.ConsoleHandler. So that is reason you can see why it is logging everything in console. Pretty straight forward , Ya ?
That's it. I hope i am able to write properly and hope it is easy to understand. Please do let me know you feedback.
Thanks
No comments:
Post a Comment