首页 \ 问答 \ 添加多个日志文件(Adding multiple log files)

添加多个日志文件(Adding multiple log files)

我想使用默认记录器以及只记录我定义的记录器的特定记录器。 我通过提到另一个SO问题得到了以下内容。

例如:

  1. log.log - 包括所有日志
  2. foo.log - 包含foo.info(..)定义的日志

我已设置以下属性

log4j.rootLogger = INFO, FOO, file

log4j.logger.FOO=DEBUG, FOO 
log4j.logger.file=DEBUG, file 

log4j.additivity.FOO=false 
log4j.additivity.file=false 

log4j.appender.FOO = org.apache.log4j.RollingFileAppender 
log4j.appender.FOO.layout = org.apache.log4j.PatternLayout
log4j.appender.FOO.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.FOO.File = foo.log

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

我的代码包括以下内容:

private static final Logger foo = Logger.getLogger("FOO");

private static final Logger log = Logger.getLogger(App.class); //NOTE I can't change this

public static void main(String[] args) { 
    log.info("info");
    foo.info("fooo");
}

但这与我想要的相反

log.log只有“info”而foo.log同时具有“info”。

请注意,我无法更改默认log实例化代码段。


I want to use a default logger as well as a specific logger that will log only the ones defined by me. I got the following by referring to another SO question.

Eg:

  1. log.log - include all logs
  2. foo.log - include logs defined by foo.info(..)

I have set the following properties

log4j.rootLogger = INFO, FOO, file

log4j.logger.FOO=DEBUG, FOO 
log4j.logger.file=DEBUG, file 

log4j.additivity.FOO=false 
log4j.additivity.file=false 

log4j.appender.FOO = org.apache.log4j.RollingFileAppender 
log4j.appender.FOO.layout = org.apache.log4j.PatternLayout
log4j.appender.FOO.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.FOO.File = foo.log

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

And my code includes the following:

private static final Logger foo = Logger.getLogger("FOO");

private static final Logger log = Logger.getLogger(App.class); //NOTE I can't change this

public static void main(String[] args) { 
    log.info("info");
    foo.info("fooo");
}

But this gives the opposite of what I want

log.log has only "info" while foo.log has both.

Note that I cannot change the default log instantiating code segment.


原文:https://stackoverflow.com/questions/49132080
更新时间:2019-12-13 08:37

最满意答案

更改您的属性文件,如下所示。 fileAppender将添加到FOO logger和root logger中以记录所有消息。 FOOAppender将仅添加到FOO记录器中以仅记录其消息。 为记录器和appender提供不同的名称,有助于理解日志属性文件。

log4j.rootLogger = INFO,fileAppender
log4j.logger.FOO=DEBUG,FOOAppender,fileAppender
log4j.additivity.FOO=false 

log4j.appender.FOOAppender = org.apache.log4j.RollingFileAppender 
log4j.appender.FOOAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.FOOAppender.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.FOOAppender.File = foo.log

log4j.appender.fileAppender=org.apache.log4j.FileAppender
log4j.appender.fileAppender.File=log.log
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Change your properties file like below. fileAppender will be added to both FOO logger and root logger to log all messages. FOOAppender will only be added to FOO logger to only log it's messages. Giving loggers and appenders different names, helps understanding log property files.

log4j.rootLogger = INFO,fileAppender
log4j.logger.FOO=DEBUG,FOOAppender,fileAppender
log4j.additivity.FOO=false 

log4j.appender.FOOAppender = org.apache.log4j.RollingFileAppender 
log4j.appender.FOOAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.FOOAppender.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.FOOAppender.File = foo.log

log4j.appender.fileAppender=org.apache.log4j.FileAppender
log4j.appender.fileAppender.File=log.log
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
2018-03-06

相关问答

更多

脚本拼图

最新问答

更多
  • NumPy标量,广播和__iadd__方法(NumPy scalars, broadcasting and the __iadd__ method)
  • 快速排序算法输出错误(Getting wrong output for quicksort algorithm)
  • Rails管理员:没有Rails 4应用程序的编辑,显示或删除按钮(Rails Admin: No edit, show, or delete buttons for Rails 4 app)
  • 部分刷新问题触发两次(Issue with partial refresh triggered twice)
  • jsPlumb draggable element javascript函数(jsPlumb draggable element javascript function)
  • MVC4:ViewModel(带有radiobuttonlist)在HttpPost之后为空(MVC4: ViewModel (with radiobuttonlist) is empty after HttpPost)
  • 如何在同一帐户上设置“Dev repo”(在prod和团队之间)(How to set up a “Dev repo” (between the prod and the team) on the same account)
  • 如何在tcl中将eth0配置为发送方udp端口(how to configure eth0 as a sender udp port in tcl)
  • EF6.1 beta 1如何获得Tooling Consolidation功能?(EF6.1 beta 1 how to get the Tooling Consolidation feature?)
  • 适用于Windows和PHP 5.5的Phalcon(Phalcon for Windows and PHP 5.5)
  • 有没有办法忽略hg责任中的提交?(Is there a way to ignore a commit in hg blame?)
  • Desire2通过API学习访问学期的权限(Desire2Learn Permissions to Access Semesters Via API)
  • 从aspx将服务器的文件复制到另一台服务器(Copy a file of a server to another server from aspx)
  • 使用YSlow / Web Developer扩展程序确定页面大小(Figuring out page size with YSlow / Web Developer extension)
  • Select语句将字符串中的前7个字符与其他表中的其他7个字符进行比较(Select statement which compares first 7 chars in a string with other 7 chars in other table)
  • 检测对注册表的更改,防止更改等(Detect changes to registry, prevent changes, etc)
  • 用户的鼠标/键盘运动分析是否有特殊名称?(Does the analysis of mouse/keyboard motions of a user have a special name?)
  • 获取Rails中资源的路由列表(Get list of routes for a resource in Rails)
  • 用函数值除以数字(Divide a function value by a number)
  • 熊猫Python:如何从今天开始创建失效列?(Pandas Python: How to create lapse since today column?)
  • GDK Bug报告(GDK Bug Reports)
  • 困惑于在python中循环遍历列表(confused about looping through lists in python)
  • 'less'由'which'输出指定的文件('less' the file specified by the output of 'which')
  • 如何在ASP.NET中优化AJAX(How to optimize AJAX in ASP.NET)
  • 在文本文件中转换纪元时间(Convert epoch times in text file)
  • 表细胞伸展(Table cell stretching)
  • GridView控件(GridView Control)
  • 从构建定义文件控制pdb文件输出(Control pdb file output from build defintion file)
  • *将方法传递给类(*Passing a Method into a Class)
  • 如何在HttpReceiveHttpRequest()中使用HTTPS?(How to use HTTPS with HttpReceiveHttpRequest()?)