java 配置 log4j 属性文件以存储在 mysql 数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7214140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configuring log4j property file to store in mysql Database
提问by Neo182
It's been a while that I began with log4j; pretty cool logging framework. I've done other type of logging like Console and File Logging. So trying for DB Adapters with mysql for Database logging. Accordingly, I've created following property file named log4j.properties as -
我开始使用 log4j 已经有一段时间了;非常酷的日志框架。我已经完成了其他类型的日志记录,例如控制台和文件日志记录。所以尝试使用 mysql 的数据库适配器进行数据库日志记录。因此,我创建了以下名为 log4j.properties 的属性文件 -
# Define the root logger with appender file
log4j.rootLogger = DEBUG, DB
# Define the DB appender
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
# Set JDBC URL
log4j.appender.DB.URL=jdbc:mysql://localhost:3306/test
# Set Database Driver
log4j.appender.DB.driver=com.mysql.jdbc.Driver
# Set database user name and password
log4j.appender.DB.user=root
log4j.appender.DB.password=
# Set the SQL statement to be executed.
log4j.appender.DB.sql=insert into log(date,level,message) values("%d","%p","%m")
# Define the layout for file appender
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
And used it in a test class in following way -
并以下列方式在测试类中使用它 -
public class DBLoggerTest {
static Logger logger;
public DBLoggerTest() {
//System.setProperty("log4j.configuration", "log4j.properties");
logger = Logger.getLogger(DBLoggerTest.class.getName());
}
public static void main(String[] args) {
new DBLoggerTest();
logger.info("This is a test info");
logger.error("This is an error messsage");
}
}
But I got following error -
但是我遇到了以下错误-
log4j:WARN No appenders could be found for logger (com.satyam.logger.test.DBLoggerTest).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Any help please...?
有什么帮助吗...?
回答by zeddarn
If you are using mysql. create a file log4j.properties. This worked for me. Put this at the root folder of you application. i.e root of all packages. I also do have a table logs with fields id,date,user, message and class.
如果您使用的是 mysql。创建文件 log4j.properties。这对我有用。把它放在你的应用程序的根文件夹中。即所有包的根目录。我也有一个包含字段 id、date、user、message 和 class 的表日志。
log4j.rootLogger=DEBUG,DB
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DB.URL=jdbc:mysql://localhost:3306/test
log4j.appender.DB.user=root
log4j.appender.DB.password=root
log4j.appender.DB.sql=INSERT INTO logs(date, user, message,class) VALUES ('%d{yyyy-MM-dd HH:mm:ss}', '%X{User}','%m','%c')
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=INSERT INTO logs (date, user,message,class) VALUES ('%d{yyyy-MM-dd HH:mm:ss}', '%X{User}','%m','%c')
log4j.category.ke.co=ERROR
log4j.category.ke.co.appender-ref=DB
Then use it as follows.
然后如下使用。
package com.zeddarn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
public class MySQLDatabaseConnector {
static ThreadLocal<Connection> connection = new ThreadLocal<Connection>();
private static Logger logger = Logger.getLogger(MySQLDatabaseConnector.class);
public static Connection getDBConnection() {
//check if a mysql connection already exits. This is to avoid reconnecting
if (connection.get() == null) {
try {
//loading the mysql driver. This means you also have to add mysql libary. You can add manually or via maven
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
//do something to deal with the error of missing mysql driver e.g notification to the user.
MDC.put("User", "loggeduser");
logger.error(e.getMessage());
MDC.getContext().clear();
}
try {
connection.set(DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"));
} catch (SQLException e) {
MDC.put("User", "loggeduser");
logger.error(e.getMessage());
MDC.getContext().clear();
}
}
return connection.get();
}
public static void main(String args[]) {
MDC.put("User", "loggeduser");
logger.error("message from exception.getMessage() method");
MDC.getContext().clear();
}
}
}