包装 log4j 或创建自定义记录器? [英] Wrap log4j or create custom logger?

查看:48
本文介绍了包装 log4j 或创建自定义记录器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要记录两种不同类型消息的应用程序:应用程序日志消息和审核消息.应用程序日志消息与标准的lo4j Logger 完全匹配,但是审核日志有几个必需的参数.

I have an application that needs to log two different types of messages: application log messages and audit messages. Application log messages match the standard lo4j Logger perfectly, but there are several required parameters for the audit log.

我认为我需要包装log4j以将其他必需的参数添加到 debug() info()等方法中,但是我讨厌包装的想法log4j.我应该:

I think I need to wrap log4j to add the additional required parameters to the debug(), info(), etc methods, but I hate the idea of wrapping log4j. Should I:

  1. 完全包装log4j并提供我自己的 Logger 类,该类在后台调用log4j记录器?
  2. 扩展log4j Logger 类并添加具有我所需参数的审核日志"方法吗?
  3. 做一些更优雅的事情,这样我就不会包装日志库...
  1. Wrap log4j completely and provide my own Logger class that calls a log4j logger behind the scenese?
  2. Extend the log4j Logger class and add "audit log" methods with my required parameters?
  3. Do something even more elegant so I'm not wrapping a logging library...

推荐答案

我认为您可以将log4j用于应用程序日志,以创建日志,例如:

I think you can use log4j for your application logs creating logs like:

private final static Logger log = new Logger(MyClass.class);

然后为您的审核日志创建一个特定类别:

And for your audit log create a specific category:

private final static Logger log = new Logger("AuditTrail");

拥有一个可以在多个类中共享的不同 appender 将允许您在配置文件中根据需要配置输出目标和格式.

Having a different appender you can share accross multiple classes will allow you to configure the output destination and the format as ever you want in the config file.

希望这会有所帮助.

这篇关于包装 log4j 或创建自定义记录器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆