我尝试登录类别,但是我不知道如何配置log4j.properties。
我到底要什么?
这是我所拥有的:
public class CategoryLogger {
private static Logger securityLogger = Logger.getLogger("SECURITY");
private static Logger databaseLogger = Logger.getLogger("DATABASE");
private static Logger mailLogger = Logger.getLogger("MAIL");
public void securitylog(LogLevel level, String message) {
this.log(securityLogger, level, message);
}
public void databaselog(LogLevel level, String message) {
this.log(databaseLogger, level, message);
}
public void maillog(LogLevel level, String message) {
this.log(mailLogger, level, message);
}
private void log(Logger logger, LogLevel level, String message) {
switch (level) {
case TRACE:
logger.trace(message);
break;
case DEBUG:
logger.debug(message);
break;
case INFO:
logger.info(message);
break;
case WARN:
logger.warn(message);
break;
case ERROR:
logger.error(message);
break;
case FATAL:
logger.fatal(message);
break;
default:
break;
}
}
}
日志应如下所示:
database-2018-09-28.log
DATABASE | 2018-09-28 20:02:19 | INFO | DAO:22 | This is a info.
DATABASE | 2018-09-28 20:02:19 | WARN | DAO:23 | This is a warn.
DATABASE | 2018-09-28 20:02:19 | ERROR | DAO:24 | This is a error.
DATABASE | 2018-09-28 20:02:19 | FATAL | DAO:25 | This is a fatal.
security-2018-09-28.log
SECURITY | 2018-09-28 20:02:19 | INFO | SimpleAccess:33 | This is a info.
SECURITY | 2018-09-28 20:02:19 | WARN | SimpleAccess:34 | This is a warn.
SECURITY | 2018-09-28 20:02:19 | ERROR | SimpleAccess:35 | This is a error.
SECURITY | 2018-09-28 20:02:19 | FATAL | SimpleAccess:36 | This is a fatal.
mail-2018-09-28.log
MAIL | 2018-09-28 20:02:19 | INFO | MailSender:44 | This is a info.
MAIL | 2018-09-28 20:02:19 | WARN | MailSender:45 | This is a warn.
MAIL | 2018-09-28 20:02:19 | ERROR | MailSender:46 | This is a error.
MAIL | 2018-09-28 20:02:19 | FATAL | MailSender:47 | This is a fatal.
非常感谢您的帮助!
问候,
L。
答案 0 :(得分:1)
一种可能的解决方案是使用3个单独的文件追加器(每个记录器一个)
例如数据库日志 将以下内容添加到您的log4j.properties
if (curl_errno($ch)){
echo 'Request Error:' . curl_error($ch);
}
您必须为安全和邮件记录器重复 appender部分