简体   繁体   中英

log4j logs into different files for same package based on where it was called from

lets say my project has 3 packages and I am using log4j for logging. org.a, org.b, org.c where org.c has some utils code which classes in package 'a' and package 'b' calls.

If I configure logs from org.a to go to file1 and from org.b to go to file2. I would like the logs from package 'c' to go into file1 if the caller of 'c' was some class in 'org.a' and into file2 if the caller of 'c' was some class in 'org.b'.

Please let me know if there is any configuration based or programmatic way to do this.

Underlying Main Problem :

I am working on apache ambari project where views are deployed as jar/war files. Each view defines a unique package name and all its code is inside that package. Ambari project just loads these jars/wars using separate class loaders. But log4j is instantiated by main Ambari project. I have configured separate log files for each view based on their package and loaded them dynamically using PropertyConfigurator. The problem is currently the logs from common packages like /contrib/views/commons goes into Ambari's log file and not in individual view's log file. If I would configure it based on package name then all the logs from these package will go into any one of the view's log file and not based on caller view.

如果在通用模块的日志文件中打印呼叫者信息怎么办?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM