配置log4j以在运行时记录到自定义文件 [英] configure log4j to log to custom file at runtime
本文介绍了配置log4j以在运行时记录到自定义文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以指导我如何配置log4j以记录我在运行时指定的特定文件。日志文件的名称和路径是在运行时生成的,应用程序必须登录到特定文件。
Can anyone please guide me as to how I can configure log4j to log to a specific file that I specify at run-time.The name and path of the log file are generated at run-time and the application must log to that particular file.
通常,log4j.properties文件中的文件appender条目指向应用程序将使用的日志文件。但是在这种情况下我想阅读从命令行和日志到该特定文件的日志文件路径。
Generally file appender entries in the log4j.properties file points to the log file that will be used by the application.However in this case I would like to read the log file path from the command line and log to that particular file.
我如何实现这一目标?
推荐答案
改编自log4j文档:
Adapted from the log4j documentation:
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;
public class SimpandFile {
static Logger logger = Logger.getLogger(SimpandFile.class);
public static void main(String args[]) {
// setting up a FileAppender dynamically...
SimpleLayout layout = new SimpleLayout();
FileAppender appender = new FileAppender(layout,"your filename",false);
logger.addAppender(appender);
logger.setLevel((Level) Level.DEBUG);
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}
这篇关于配置log4j以在运行时记录到自定义文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文