正确 配置和使用 的 log4j.properties的 方法
http://plane.javaeye.com/blog/288886
- # Set log levels #
- log4j.rootLogger = DEBUG, Console, LogFile, ErrorFile
- # Output the log info to the Java Console #
- log4j.appender.Console = org.apache.log4j.ConsoleAppender
- log4j.appender.Console.Target = System.out
- log4j.appender.Console.ImmediateFlush = true
- log4j.appender.Console.Threshold = DEBUG
- log4j.appender.Console.layout = org.apache.log4j.PatternLayout
- log4j.appender.Console.layout.ConversionPattern = <%-d{yyyy-MM-dd HH:mm:ss}> <%t> %l %n<%p> %m%n%n
- # Save the log info to the log file #
- log4j.appender.LogFile = org.apache.log4j.RollingFileAppender
- log4j.appender.LogFile.File = logs/log.log
- log4j.appender.LogFile.Append = true
- log4j.appender.LogFile.ImmediateFlush = false
- log4j.appender.LogFile.MaxFileSize = 8MB
- log4j.appender.LogFile.MaxBackupIndex = 256
- log4j.appender.LogFile.Threshold = INFO
- log4j.appender.LogFile.layout = org.apache.log4j.PatternLayout
- log4j.appender.LogFile.layout.ConversionPattern = <%-d{yyyy-MM-dd HH:mm:ss}> <%t> %l %n<%p> %m%n%n
- # Save the error info to the error file. A file one day. #
- log4j.appender.ErrorFile = org.apache.log4j.DailyRollingFileAppender
- log4j.appender.ErrorFile.File = logs/error.log
- log4j.appender.ErrorFile.Append = true
- log4j.appender.ErrorFile.ImmediateFlush = false
- log4j.appender.ErrorFile.Threshold = ERROR
- log4j.appender.ErrorFile.layout = org.apache.log4j.PatternLayout
- log4j.appender.ErrorFile.layout.ConversionPattern = <%-d{yyyy-MM-dd HH:mm:ss}> <%t> %l %n<%p> %m%n%n
# Set log levels # log4j.rootLogger = DEBUG, Console, LogFile, ErrorFile # Output the log info to the Java Console # log4j.appender.Console = org.apache.log4j.ConsoleAppender log4j.appender.Console.Target = System.out log4j.appender.Console.ImmediateFlush = true log4j.appender.Console.Threshold = DEBUG log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern = <%-d{yyyy-MM-dd HH:mm:ss}> <%t> %l %n<%p> %m%n%n # Save the log info to the log file # log4j.appender.LogFile = org.apache.log4j.RollingFileAppender log4j.appender.LogFile.File = logs/log.log log4j.appender.LogFile.Append = true log4j.appender.LogFile.ImmediateFlush = false log4j.appender.LogFile.MaxFileSize = 8MB log4j.appender.LogFile.MaxBackupIndex = 256 log4j.appender.LogFile.Threshold = INFO log4j.appender.LogFile.layout = org.apache.log4j.PatternLayout log4j.appender.LogFile.layout.ConversionPattern = <%-d{yyyy-MM-dd HH:mm:ss}> <%t> %l %n<%p> %m%n%n # Save the error info to the error file. A file one day. # log4j.appender.ErrorFile = org.apache.log4j.DailyRollingFileAppender log4j.appender.ErrorFile.File = logs/error.log log4j.appender.ErrorFile.Append = true log4j.appender.ErrorFile.ImmediateFlush = false log4j.appender.ErrorFile.Threshold = ERROR log4j.appender.ErrorFile.layout = org.apache.log4j.PatternLayout log4j.appender.ErrorFile.layout.ConversionPattern = <%-d{yyyy-MM-dd HH:mm:ss}> <%t> %l %n<%p> %m%n%n
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import org.apache.log4j.BasicConfigurator;
- import org.apache.log4j.Level;
- import org.apache.log4j.LogManager;
- import org.apache.log4j.Logger;
- import org.apache.log4j.Priority;
- import org.apache.log4j.PropertyConfigurator;
- public class EncapsulationLog4J
- {
- public static final String PROFILE = "log4j.properties";
- private static EncapsulationLog4J encapsulationLog4J;
- static
- {
- encapsulationLog4J = new EncapsulationLog4J();
- }
- private Logger log4j;
- private EncapsulationLog4J()
- {
- log4j = LogManager.getLogger(EncapsulationLog4J.class);
- Properties pro = new Properties();
- InputStream is = getClass().getResourceAsStream(PROFILE);
- try
- {
- pro.load(is);
- } catch (IOException e)
- {
- BasicConfigurator.configure();
- e.printStackTrace();
- }
- PropertyConfigurator.configure(pro);
- }
- public void log(String level,Object msg,Throwable e)
- {
- if(null!=log4j)
- {
- log4j.log((Priority)Level.toLevel(level), msg, e);
- }
- }
- public void log(String level,Object msg)
- {
- log(level,msg,null);
- }
- public void log(String level,Throwable e)
- {
- log(level,null,e);
- }
- public static EncapsulationLog4J getInstance()
- {
- return encapsulationLog4J;
- }
- }
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Level; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.Priority; import org.apache.log4j.PropertyConfigurator; public class EncapsulationLog4J { public static final String PROFILE = "log4j.properties"; private static EncapsulationLog4J encapsulationLog4J; static { encapsulationLog4J = new EncapsulationLog4J(); } private Logger log4j; private EncapsulationLog4J() { log4j = LogManager.getLogger(EncapsulationLog4J.class); Properties pro = new Properties(); InputStream is = getClass().getResourceAsStream(PROFILE); try { pro.load(is); } catch (IOException e) { BasicConfigurator.configure(); e.printStackTrace(); } PropertyConfigurator.configure(pro); } public void log(String level,Object msg,Throwable e) { if(null!=log4j) { log4j.log((Priority)Level.toLevel(level), msg, e); } } public void log(String level,Object msg) { log(level,msg,null); } public void log(String level,Throwable e) { log(level,null,e); } public static EncapsulationLog4J getInstance() { return encapsulationLog4J; } }
- public class Log
- {
- private static EncapsulationLog4J log = EncapsulationLog4J.getInstance();
- public static void logError(String msg)
- {
- log.log("ERROR", msg);
- }
- public static void logError(Throwable e)
- {
- log.log("ERROR", e);
- }
- public static void logWarn(String msg)
- {
- log.log("WARN", msg);
- }
- public static void logWarn(Throwable e)
- {
- log.log("WARN", e);
- }
- public static void logInfo(String msg)
- {
- log.log("INFO", msg);
- }
- public static void logInfo(Throwable e)
- {
- log.log("INFO", e);
- }
- public static void logDebug(String msg)
- {
- log.log("DEBUG", msg);
- }
- public static void logDebug(Throwable e)
- {
- log.log("DEBUG", e);
- }
- }
public class Log { private static EncapsulationLog4J log = EncapsulationLog4J.getInstance(); public static void logError(String msg) { log.log("ERROR", msg); } public static void logError(Throwable e) { log.log("ERROR", e); } public static void logWarn(String msg) { log.log("WARN", msg); } public static void logWarn(Throwable e) { log.log("WARN", e); } public static void logInfo(String msg) { log.log("INFO", msg); } public static void logInfo(Throwable e) { log.log("INFO", e); } public static void logDebug(String msg) { log.log("DEBUG", msg); } public static void logDebug(Throwable e) { log.log("DEBUG", e); } }
- public class TestLog
- {
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- Log.logDebug("test log debug");
- Log.logError("test Error");
- }
- }
public class TestLog { /** * @param args */ public static void main(String[] args) { Log.logDebug("test log debug"); Log.logError("test Error"); } }
还有一种方式 配置文件log4j.properties 放在src下
- import java.net.URL;
- import org.apache.log4j.BasicConfigurator;
- import org.apache.log4j.Level;
- import org.apache.log4j.Logger;
- import org.apache.log4j.Priority;
- import org.apache.log4j.PropertyConfigurator;
- public class LoggerService
- {
- private Logger logger;
- public LoggerService(String location)
- {
- logger = (Logger) Logger.getLogger(location);
- //读取配置文件
- getLog4jConfig();
- }
- public void debug(Object msg)
- {
- log("DEBUG", msg, null);
- }
- public void debug(Object msg, Throwable t)
- {
- log("DEBUG", msg, t);
- }
- public void info(Object msg)
- {
- log("INFO", msg, null);
- }
- public void info(Object msg, Throwable t)
- {
- log("INFO", msg, t);
- }
- public void warn(Object msg)
- {
- log("WARN", msg, null);
- }
- public void warn(Object msg, Throwable t)
- {
- log("WARN", msg, t);
- }
- public void error(Object msg)
- {
- log("ERROR", msg, null);
- }
- public void error(Object msg, Throwable t)
- {
- log("ERROR", msg, t);
- }
- private void log(String level, Object msg, Throwable e)
- {
- if (null != logger)
- {
- logger.log((Priority) Level.toLevel(level), msg, e);
- }
- }
- /**
- * 读取配置文件
- */
- private void getLog4jConfig()
- {
- if (System.getProperty("log4j.configuration") == null)
- {
- URL url = ClassLoader.getSystemResource("log4j.properties");
- String path = "";
- if(null!=url)
- {
- path = url.getPath();
- }
- PropertyConfigurator.configure(path);
- BasicConfigurator.configure();
- }
- }
- }
import java.net.URL; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.Priority; import org.apache.log4j.PropertyConfigurator; public class LoggerService { private Logger logger; public LoggerService(String location) { logger = (Logger) Logger.getLogger(location); //读取配置文件 getLog4jConfig(); } public void debug(Object msg) { log("DEBUG", msg, null); } public void debug(Object msg, Throwable t) { log("DEBUG", msg, t); } public void info(Object msg) { log("INFO", msg, null); } public void info(Object msg, Throwable t) { log("INFO", msg, t); } public void warn(Object msg) { log("WARN", msg, null); } public void warn(Object msg, Throwable t) { log("WARN", msg, t); } public void error(Object msg) { log("ERROR", msg, null); } public void error(Object msg, Throwable t) { log("ERROR", msg, t); } private void log(String level, Object msg, Throwable e) { if (null != logger) { logger.log((Priority) Level.toLevel(level), msg, e); } } /** * 读取配置文件 */ private void getLog4jConfig() { if (System.getProperty("log4j.configuration") == null) { URL url = ClassLoader.getSystemResource("log4j.properties"); String path = ""; if(null!=url) { path = url.getPath(); } PropertyConfigurator.configure(path); BasicConfigurator.configure(); } } }
- public class Log
- {
- public static LoggerService getLoggerService(String name) {
- return new LoggerService(name);
- }
- }
public class Log { public static LoggerService getLoggerService(String name) { return new LoggerService(name); } }
- public class TestLog
- {
- public static void main(String[] args)
- {
- LoggerService log = Log.getLoggerService(TestLog.class.getName());
- log.warn("this is test logger");
- }
- }
public class TestLog { public static void main(String[] args) { LoggerService log = Log.getLoggerService(TestLog.class.getName()); log.warn("this is test logger"); } }
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.Properties;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import org.apache.log4j.Logger;
- import org.apache.log4j.PropertyConfigurator;
- public class Log4jInit extends HttpServlet
- {
- private static final long serialVersionUID = -4499302208753939187L;
- static Logger logger = Logger
- .getLogger(Log4jInit.class);
- public void init(ServletConfig config) throws ServletException
- {
- String prefix = config.getServletContext().getRealPath("/");
- String file = config.getInitParameter("log4j");
- String filePath = prefix + file;
- Properties props = new Properties();
- try
- {
- FileInputStream istream = new FileInputStream(filePath);
- props.load(istream);
- istream.close();
- //设置路径
- // String logFile = prefix + props.getProperty("log4j.appender.file.File");
- // System.out.println("日志输出文件路径:"+logFile);
- // props.setProperty("log4j.appender.file.File",logFile);
- // 装入log4j配置信息
- PropertyConfigurator.configure(props);
- System.out.println("装载完成log4j.properties:"+filePath);
- }
- catch (IOException e)
- {
- System.out.println("Could not read configuration file [" + filePath
- + "].");
- System.out.println("Ignoring configuration file [" + filePath
- + "].");
- return;
- }
- }
- }
import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class Log4jInit extends HttpServlet { private static final long serialVersionUID = -4499302208753939187L; static Logger logger = Logger .getLogger(Log4jInit.class); public void init(ServletConfig config) throws ServletException { String prefix = config.getServletContext().getRealPath("/"); String file = config.getInitParameter("log4j"); String filePath = prefix + file; Properties props = new Properties(); try { FileInputStream istream = new FileInputStream(filePath); props.load(istream); istream.close(); //设置路径 // String logFile = prefix + props.getProperty("log4j.appender.file.File"); // System.out.println("日志输出文件路径:"+logFile); // props.setProperty("log4j.appender.file.File",logFile); // 装入log4j配置信息 PropertyConfigurator.configure(props); System.out.println("装载完成log4j.properties:"+filePath); } catch (IOException e) { System.out.println("Could not read configuration file [" + filePath + "]."); System.out.println("Ignoring configuration file [" + filePath + "]."); return; } } }
- <servlet>
- <servlet-name>log4j-init</servlet-name>
- <servlet-class>
- com.web.serverlet.Log4jInit
- </servlet-class>
- <init-param>
- <param-name>log4j</param-name>
- <param-value>WEB-INF/classes/log4j.properties</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
<servlet> <servlet-name>log4j-init</servlet-name> <servlet-class> com.web.serverlet.Log4jInit </servlet-class> <init-param> <param-name>log4j</param-name> <param-value>WEB-INF/classes/log4j.properties</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>