Java log4j同时写入文本日志和数据库日志

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




Log4jUtil.java

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;

public class Log4jUtil {
	
	private static Logger logger = Logger.getLogger(Log4jUtil.class.getName());
	/**
	 * 增删改动作的日志写入 (谁 操作 什么)
	 * @param op 用户操作
	 * @param param 传入参数
	 */
	public static void logInfo(String op, String param) {
		if( (op.equals("")||op==null) || (param.equals("")||param==null)){
			System.out.println("没有参数传入");
		}
		else{
			MDC.put("user", "session.id"); // 用户id
			MDC.put("operation", op); // 用户操作
			MDC.put("param", param); // 接收参数

			logger.info("");

			MDC.remove("user");
			MDC.remove("operation");
			MDC.remove("param");
		}
	}
	
	/**
	 * 发生异常的日志写入
	 * @param ex 异常信息
	 */
	public static void logError(String ex) {
		logger.error(ex);
	}
}

log4j.properties(放在src目录下)

### 设置级别和目的地(这里多个目的地) ###
log4j.rootLogger = CONSOLE,InfoLog,ErrorLog,logDB

### 输出到控制台 ###
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target = System.out
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{1}:%L [%t:%r]- %m%n

 ### 输出文件info ###
 log4j.logger.infolog = InfoLog
 log4j.appender.InfoLog = org.apache.log4j.DailyRollingFileAppender
 log4j.appender.InfoLog.File = logs/info.log
 log4j.appender.InfoLog.Append = true
 log4j.appender.InfoLog.Threshold = INFO
 log4j.appender.InfoLog.DatePattern = '.'yyyy-MM
 log4j.appender.InfoLog.layout = org.apache.log4j.PatternLayout
 log4j.appender.InfoLog.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ] - [%X{user} %X{operation} %X{param}] - %m%n

 ### 输出文件error ###
 
 log4j.logger.errorlog = ErrorLog
 log4j.appender.ErrorLog = org.apache.log4j.DailyRollingFileAppender
 log4j.appender.ErrorLog.File = logs/error.log
 log4j.appender.ErrorLog.Append = true
 log4j.appender.ErrorLog.Threshold = ERROR
 log4j.appender.ErrorLog.DatePattern = '.'yyyy-MM
 log4j.appender.ErrorLog.layout = org.apache.log4j.PatternLayout
 log4j.appender.ErrorLog.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss}  [ %t\:%r ] - [ %p ] - [%X{user} %X{operation} %X{param} - %m%n
 
 #将日志输出到SQLserver数据库 
log4j.appender.logDB = org.apache.log4j.jdbc.JDBCAppender
log4j.appender.logDB.layout = org.apache.log4j.PatternLayout
log4j.appender.logDB.Driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
log4j.appender.logDB.URL = jdbc:sqlserver://localhost:1433;DatabaseName=hibernatetest
log4j.appender.logDB.User = sa
log4j.appender.logDB.Password = 123456
log4j.appender.logDB.Sql =INSERT INTO T_Log(create_date,[level],category,file_name,thread_name,line,all_category,message,[user],operation,param)values('%d{yyyy-MM-dd HH\:mm\:ss}','%p','%c','%F','%t','%L','%l','%m','%X{user}','%X{operation}','%X{param}') 

Sql脚本(SQL Server)

CREATE TABLE [dbo].[T_Log](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[create_date] [datetime] NULL,
	[level] [varchar](50) NULL,
	[category] [varchar](50) NULL,
	[file_name] [varchar](50) NULL,
	[thread_name] [varchar](50) NULL,
	[line] [int] NULL,
	[all_category] [varchar](50) NULL,
	[message] [varchar](50) NULL,
	[user] [varchar](50) NULL,
	[operation] [varchar](50) NULL,
	[param] [varchar](50) NULL,
 CONSTRAINT [PK_T_Log] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

相关jar包

log4j.jar、mssql-jdbc.jar

注意:

log4j.properties文件的数据库连接根据自己的数据库来

原文链接:https://www.idaobin.com/archives/2106.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

电子邮件地址不会被公开。 必填项已用*标注

− 5 = 2