I am novice in log4j framework. I was going through tutorial they all say that "Loggers are stored in a namespace hierarchy " ?
Is it some this related to object instantiation of loggers?
And what logger.getLogger(App.class) actually does? does it create a separate instance of logger for App.class?

- 1,739
- 23
- 50

- 518
- 1
- 6
- 22
2 Answers
The Logger object is main object to log any message. Logger objects encapsulate logging messages and do not have any information about destination or formatting.
The Logger objects acting within a particular instance of app follow a PARENT-CHILD hierarchy.
At the top of the hierarchy a root logger exist. This root logger exists outside the scope of custom logger hierarchy. All the other application-specific Logger objects are child objects to the root logger. This parent-child relationship of logger signifies the dependency of loggers acting within the same application. A child Logger can inherit properties from its parent loggers.

- 1,406
- 2
- 18
- 33
-
So if i have 20 class in com.bar package .So i will have 20 loggers instances they all be under com.bar logging level. So if i set logging level of com.bar to info they only info message will be displayed for com.bar. is my under standing right? Pardon me if i understood wrong. – Thinker Sep 20 '17 at 06:46
-
@Prashank Your uderstanding is correct. A child logger will inherit following properties from its parents: A) Level: If child logger has no explicit tree level specified it will use the level of its closest parents. B) Appender: The child logger uses the appender of closest parent logger if no appender attached to logger. C) ResourceBundle: These are key-value pattern properties files used for the localization of logging message. A child Logger inherits any ResourceBundle associated with its parent logger. – Alex Sep 20 '17 at 07:44
-
When a new Logger instance is created, LogManager stores the instance in namespace storage with the namespace as the key. If we try to create the same Logger instance again while the application is running, the existing instance is returned. – Alex Sep 20 '17 at 07:54
-
I have one more Question. which one is better one application level logger or class level logger. for example if i create only one logger say Logger logger=Logger.getLogger("com.myapp.MY_APP"); and create a wrapper for logging messaging public void log(Class class,String message,int level){}. Won't it be smart choice. I don't want logger per class because currently i have 108 java class so there will be 108 logger instance and if one logger consume 4 byte of memory then total memory consumption will be 108*4 bytes. Any Comments? – Thinker Sep 20 '17 at 07:58
-
A logger in each class is better and more easy to extend. The reason is that define one logger in one class separate the real logging api from the logger's configuration (format, persistence) easily. Define "one logger per class" doesn't mean each class uses a different logger instance. "a logger in each class" is just define one logger in every class, But such logger may refer to the same logger instance from different classes. Already asked check here https://stackoverflow.com/a/6640407/1132791 – Alex Sep 20 '17 at 08:18
-
Hi read the content in the link stackoverflow.com/a/6640407/1132791 but i don't understand how private static final Logger log = Logger.getLogger( Foo.class ); and private static final Logger log = Logger.getLogger( Bar.class ); will share the same logger instance – Thinker Sep 20 '17 at 10:00
-
as both Foo.class and Bar.class will have sperate key example if both are in com.test package then key for Foo.class will con.test.Foo.class and for Bar.class it will be com.test.Bar.class. If sharing is possible then how to achieve that – Thinker Sep 20 '17 at 10:10
If you create 4 loggers "com.foo.apple", "com.foo.juice", "com.bar.tomato", "com.bar.potato" you will have hierarchy like:
com
.foo
.apple
.juice
.bar
.tomato
.potato
It comes to play when you configuring your loggers. For example LogLevel - you could set DEBUG for "com" node, set WARN for "com.foo", and set ERROR for "com.foo.juice". Logger "com.foo.apple" would be at WARN level, loggers "com.bar", "com.bar.tomato", "com.bar.potato" would be at DEBUG level in this case.

- 49
- 4
-
Thanxx, I have another question . When i execute Logger log=Logger.getLogger(MyApp.class),will it create seprate logger for MyApp.class . Note: MyApp is in com.for package – Thinker Sep 20 '17 at 06:34
-
Internally most of logging frameworks store loggers in hashtable, so if you are not called Logger.getLogger(MyApp.class) earlier, it will create new instance Logger, otherwise it will just return already created logger. – HeyHo Sep 20 '17 at 06:39
-
So if i have 20 class in com.bar package .So i will have 20 loggers instances they all be under com.bar logging level. So if i set logging level of com.bar to info they only info message will be displayed for com.bar. is my under standing right? Pardon me if i understood wrong. – Thinker Sep 20 '17 at 06:44
-
Nope, if you specify class like Logger.getLogger(MyApp.class) full qualified class name will be used as name of logger and will be "com.bar.MyApp". So if you will have 20 classes in com.bar, 20 logger instances will be created. And setting info level for "com.bar" that will affect all of them. – HeyHo Sep 20 '17 at 06:50
-
Sorry i did not understood what you just said. Suppose i have three class ClassA ,ClassB,ClassC in com.bar package . And each class have static logger instance . static logger instance in ClassA is logger logofclassA=Logger.getLogger(ClassA.class), similarly in class B, is loggerofclassB=Logger.getLogger(ClassB.class) and same in class C . So will i have 3 logger instances ? and if i set com.bar logging level to info then only info message of these three level will be visible right? – Thinker Sep 20 '17 at 06:55
-
Thanxx brother for your input let me check if they are working as understood by me. – Thinker Sep 20 '17 at 07:27