问题描述
基本上,当使用log4j时,我知道我们必须使用logger.getlogger(myclass.class)或logger.getlogger(" myclass").
我有太多的课程,我不想在每个班级中添加此语句.相反,我想做的是这个
Logger.info(this,"My message");
当logger是我编写的类(不是log4j中的一个类),然后又启动了类型logger(org.apache.log4j.logger)的对象,并继续进行消息.
这是我的logger.java
package com.mypackage; /** * @author Sriram Sridharan *Custom logging implementation using log4j */ public class Logger { private static org.apache.log4j.Logger logger; /** * Writes an INFO log * @param oClass * @param message */ public static void INFO(Object oClass, String message){ org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(oClass.getClass()); logger.info(message); } }
现在,我有自己的课程,将此记录器实现称为日志事件.看起来像这个
public class MyClass { public void myMethod(){ com.mypackage.Logger.INFO(this, "Hello, World"); } }
我对日志没有问题.他们很好.但是,日志中显示的类名是相同的(Logger).这是输出
INFO [main] (Logger.java:18) - Hello, World
我希望这个
INFO [main] (MyClass.java:18) - Hello, World
我要去哪里?
推荐答案
使用%c输出类别的模式/getLogger("myCategory")/而不是%C打印呼叫者类.
这个想法是您有通用方法:
public static void debug(String category, String message) { Logger logger = org.apache.log4j.Logger.getLogger(category); logger.debug(message); }
和您的log4j配置包含您的模式:
%d{ISO8601} %c %m %n
这将打印
2014-02-21 14:38:120 YourCategory Your Message
因此,您无需使用日志来弄乱每个类的记录仪的Intatiation.
其他推荐答案
您正在使用logger.info(message); logger.info(message); Logger class,所以始终Logger.java您将进入日志文件.
您可以记录要登录的类中的消息,将记录类名.
其他推荐答案
您不使用类参数获得Log4J Logger,因此Log4J假定Logger类. 尝试
public static void INFO(Object oClass, String message){ org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(oClass.getClass()); logger.info(message); }
而不是.
问题描述
Basically, when using log4j, I know we have to initiate a Logger by using Logger.getLogger(MyClass.class) or Logger.getLogger("MyClass").
I have too many classes and I dont want to add this statement in every class. Rather, what I woudl like to do is this
Logger.info(this,"My message");
Where Logger is a class I have written (not the one in log4j) which in turn initiates an object of type Logger (org.apache.log4j.Logger) and carries on with the message.
This is my Logger.java
package com.mypackage; /** * @author Sriram Sridharan *Custom logging implementation using log4j */ public class Logger { private static org.apache.log4j.Logger logger; /** * Writes an INFO log * @param oClass * @param message */ public static void INFO(Object oClass, String message){ org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(oClass.getClass()); logger.info(message); } }
Now, I have my own class, that calls this Logger implementation to log events. it looks like this
public class MyClass { public void myMethod(){ com.mypackage.Logger.INFO(this, "Hello, World"); } }
I don't have a problem with the logs. They're fine. However, the Class name displayed in the logs is the same (Logger). This is the output
INFO [main] (Logger.java:18) - Hello, World
I expect this
INFO [main] (MyClass.java:18) - Hello, World
Where am I going wrong?
推荐答案
Use %c pattern to output category /getLogger("myCategory")/ instead of %C that prints caller class.
The idea is that you have generic method:
public static void debug(String category, String message) { Logger logger = org.apache.log4j.Logger.getLogger(category); logger.debug(message); }
and your log4j configuration contains your pattern:
%d{ISO8601} %c %m %n
and this will print
2014-02-21 14:38:120 YourCategory Your Message
So you do not need to mess up with instatiation of Logger for each class with log.
其他推荐答案
you are using logger.info(message); inside Logger class so always Logger.java you will get in your log file.
You can log the message inside the class in which you want to log, the classname will be logged.
其他推荐答案
You don't use the class parameter in obtaining the log4j logger, so log4j assumes your logger class. Try
public static void INFO(Object oClass, String message){ org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(oClass.getClass()); logger.info(message); }
instead.