自定义日志输出类log4j

以下是我自己写的一个日志类,写这个类的初衷是为了规范项目组各个组员的日志输出格式规范,为以后日志查看分析提供方便,在代码生成器里加入这个日志类的引用,这样就可以很好的避免组员自己直接调用原生的日志框架。在这里贴出了一是为了自己做备份,二也是让大家看看是不是有哪里写的不好可以改进下,如果不喜欢千万别喷,程序员很胆小的,谢谢。

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 
 * @author 小锋
 *
 */
public class MyLogger {

    private Logger LOGGER;

    // 多线程使用ConcurrentMap
    private static ConcurrentMap<Class<?>, MyLogger> conMap = new  ConcurrentHashMap<Class<?>, MyLogger>();

    public static MyLogger getMyLogger(Class<?> clazz){
        if(conMap.containsKey(clazz)){
            return conMap.get(clazz);
        }
        synchronized(MyLogger.class){
            new MyLogger().initMyLogger(clazz);
        }
        return conMap.get(clazz);
    }

    /**
     * 创建MyLogger
     * @param clazz
     * @return
     */
    public void initMyLogger(Class<?> clazz){
        synchronized(MyLogger.class){
            MyLogger log = new MyLogger();
            log.setLOGGER(LoggerFactory.getLogger(clazz));
            conMap.put(clazz, log);
        }
    }

    //***************************************//
    //***          日志操作方法自行添加      ***//                                                                
    //**************************************//

    /**
     * 普通info日志
     *
     * @param content 日志内容
     */
    @Deprecated
    public void info(String content) {
        LOGGER.info(content);
    }


    /**
     * 普通error日志
     *
     * @param content 日志描述内容
     * @param t       -异常信息
     */
    @Deprecated
    public void error(String content, Throwable t) {
        LOGGER.error(content, t);
    }
    @Deprecated
    public void error(String content) {
        LOGGER.error(content);
    }




    /**
     * 记录请求日志
     *
     * @param requestId      请求标识
     * @param requestDesc    调用描述
     * @param requestContent 请求内容
     */
    public void info(String requestId, String requestDesc, String requestContent) {
        LOGGER.info(getContent(requestId, requestDesc, requestContent));
    }


    /**
     * 记录请求日志
     *
     * @param requestId      请求标识
     * @param requestDesc    调用描述
     * @param requestContent 请求内容
     */
    public void requestLog(String requestId, String requestDesc, Object requestContent) {
        LOGGER.info("Request -> " + getContent(requestId, requestDesc, requestContent));
    }
    public void requestLog(String requestId, String requestDesc) {
        requestLog(requestId, requestDesc, requestDesc);
    }

    /**
     * 记录返回日志
     *
     * @param requestId      请求标识
     * @param requestDesc    调用描述
     * @param requestContent 请求内容
     */
    public void responseLog(String requestId, String requestDesc, String requestContent) {
        LOGGER.info("Response-> " + getContent(requestId, requestDesc, requestContent));
    }

    public void responseLog(String requestId, String requestDesc) {
        responseLog(requestId, requestDesc, requestDesc);
    }


    /**
     * 记录返回日志
     *
     * @param requestId      请求标识
     * @param requestDesc    调用描述
     * @param requestContent 请求内容
     * @param t              异常信息
     */
    public void error(String requestId, String requestDesc, String requestContent, Throwable t) {
        LOGGER.error("Exception->" + getContent(requestId, requestDesc, requestContent), t);
    }

    public void error(String requestId, String requestDesc, Throwable t) {
        error(requestId, requestDesc, requestDesc, t);
    }

    public void error(String requestId, String requestDesc) {
        LOGGER.error("Exception->" + getContent(requestId, requestDesc, requestDesc));
    }


    private String getContent(String requestId, String requestDesc, Object requestContent) {
        return "id=" + requestId + ", description=" + requestDesc + ", content=" + requestContent;
    }


    public Logger getLOGGER() {
        return LOGGER;
    }


    public void setLOGGER(Logger lOGGER) {
        LOGGER = lOGGER;
    }
}
要在 `log4j.properties` 中配置自定义日志输出路径,可以按照以下步骤进行操作: 1. 在 `log4j.properties` 文件中添加一个新的 appender,指定日志输出路径。例如: ```properties log4j.appender.custom=org.apache.log4j.RollingFileAppender log4j.appender.custom.File=/path/to/custom.log log4j.appender.custom.MaxFileSize=10MB log4j.appender.custom.MaxBackupIndex=10 log4j.appender.custom.layout=org.apache.log4j.PatternLayout log4j.appender.custom.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c %x - %m%n ``` 在上面的配置中,`custom` 是 appender 的名称,`/path/to/custom.log` 是自定义日志输出路径,`MaxFileSize` 和 `MaxBackupIndex` 分别指定文件大小和备份文件数。 2. 在 `log4j.properties` 文件中指定日志输出目的地。例如: ```properties log4j.rootLogger=INFO, custom ``` 在上面的配置中,`custom` 是上一步中定义的 appender 名称。 3. 在代码中获取 Logger 对象并使用。例如: ```java import org.apache.log4j.Logger; public class MyApplication { private static final Logger logger = Logger.getLogger(MyApplication.class); public static void main(String[] args) { logger.info("Hello, world!"); } } ``` 在上面的代码中,使用 `Logger.getLogger(MyApplication.class)` 获取 Logger 对象,并调用 `logger.info("Hello, world!")` 输出日志信息。日志信息将会输出自定义日志输出路径 `/path/to/custom.log`。 注意:如果你已经在 `log4j.properties` 文件中定义了其他的 appender日志输出目的地,需要将自定义的 appender日志输出目的地添加到现有配置中。例如: ```properties log4j.rootLogger=INFO, console, file, custom 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{ISO8601} [%t] %-5p %c %x - %m%n log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=/path/to/file.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c %x - %m%n log4j.appender.custom=org.apache.log4j.RollingFileAppender log4j.appender.custom.File=/path/to/custom.log log4j.appender.custom.MaxFileSize=10MB log4j.appender.custom.MaxBackupIndex=10 log4j.appender.custom.layout=org.apache.log4j.PatternLayout log4j.appender.custom.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c %x - %m%n ``` 在上面的配置中,`console` 和 `file` 是已经定义的 appender日志输出目的地,`custom` 是自定义的 appender日志输出目的地。注意在 `log4j.rootLogger` 中将它们都添加进去。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值