简体   繁体   English

如何在 Web 应用程序中使用 java.util.logger?

[英]How to use java.util.logger in a web application?

I'm trying to use a logger across a web application.我正在尝试在 Web 应用程序中使用记录器。 I have added the FileHandler to write the log into file.我添加了 FileHandler 将日志写入文件。 Now, I need to use the same handler across other classes/servlets in the project, so that logs from all classes are written to same text file.现在,我需要在项目中的其他类/servlet 中使用相同的处理程序,以便将所有类的日志写入相同的文本文件。 How can I achieve this?我怎样才能做到这一点?

/***
 * Initialize a logger
 */
public static Logger logger;
static {
    try {
      FileHandler fh = new FileHandler("log.txt", true);
      fh.setFormatter(new SimpleFormatter());
      logger = Logger.getLogger(MyClass.class.getName());
      logger.addHandler(fh);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
}

Do I need to initialize the logger and add handler in every class as in above code?我是否需要像上面的代码一样初始化记录器并在每个类中添加处理程序? Any other techniques?还有其他技巧吗?

package package_name;

import java.io.IOException;

import java.util.Properties;

import org.apache.log4j.Logger;

import org.apache.log4j.PropertyConfigurator;


public class Log {

public  Log() 
{
        Properties props = new Properties();
    try {
        props.load(getClass().getResourceAsStream("/log4j.properties"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    PropertyConfigurator.configure(props);//PropertyConfigurator.configure("log4j.properties");
}

public  Logger getLogger(Object obj) 
{
    Logger logger = Logger.getLogger(Object.class);
    return logger;
}

} }

then we have to maintain a log4j.properties file in one of our packages,and the file should be as follows,那么我们必须在我们的一个包中维护一个log4j.properties文件,该文件应该如下所示,

log4j.properties log4j.properties

log4j.rootLogger=DEBUG, R,CA

log4j.appender.R = org.apache.log4j.DailyRollingFileAppender

log4j.appender.R.File = c:\\our project name+LOGSLIVE\\logs\\project short name.log

log4j.appender.R.Append = true

log4j.appender.R.DatePattern = '_'yyyy-MM-dd'.log'

log4j.appender.R.layout = org.apache.log4j.PatternLayout

#log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [%t] [%p] %m%n

log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %5p [%t] (%F:%L) -  %m%n


#Console Appender

log4j.appender.CA=org.apache.log4j.ConsoleAppender

log4j.appender.CA.layout=org.apache.log4j.PatternLayout

log4j.appender.CA.layout.ConversionPattern=%5p [%t] (%F:%L) -  %m%n

I'd consider using a logging framework such as Log4J.我会考虑使用日志框架,如 Log4J。

Using it would just boil down to configuring the appenders (eg FileAppender) and log levels in a central file (.xml or .properties) and in each class that needs to define a logger you'd just do Log l = LogFactory.getLog(clazz);使用它只会归结为在中央文件(.xml 或 .properties)和每个需要定义记录器的类中配置附加程序(例如 FileAppender)和日志级别,您只需执行Log l = LogFactory.getLog(clazz); (where clazz is the class you define the logger for). (其中 clazz 是您为其定义记录器的类)。

You could make the logger public static and use it from other classes as well but I'd not recommend it, since you normally want to know which logger (ie which class that logger was defined for) generated a log entry.您可以将记录器设为 public 静态并从其他类中使用它,但我不建议这样做,因为您通常想知道哪个记录器(即为哪个类定义了该记录器)生成了一个日志条目。

You could use the logging.properties file to define your handlers globally for the whole application.您可以使用logging.properties文件为整个应用程序全局定义处理程序。 In this file you can fine-tune your logging needs.在此文件中,您可以微调您的日志记录需求。

Look here or just google for logging.properties .这里或只是谷歌logging.properties

Example from the link above:来自上面链接的示例:

handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler

java.util.logging.ConsoleHandler.level = INFO
java.util.logging.FileHandler.level = ALL

java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

You can even setup different logging behavior for each of you web applications by placing the logging.properties in WEB-INF/classes of your web app.您甚至可以通过将logging.properties放在 Web 应用程序的WEB-INF/classes 中,为每个 Web 应用程序设置不同的日志记录行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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