当我使用JavaLog4J时,它将日志文件中的条目相乘,得到1gb的日志

o2gm4chl  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(210)

当我使用javalog4j时,它将日志文件中的条目相乘,从而生成一个1gb大小的日志文件。第一次写一个条目,下一次写两个条目。t。c。

@Service
public class DynamicRollingLogFile {

public void makeLogger(String logType, String logText) {
    // Creates Pattern Layout
    // Creates Pattern Layout
    PatternLayout patternLayoutObj = new PatternLayout();
    String conversionPattern = "[%p] %d %c %M - %m%n";
    patternLayoutObj.setConversionPattern(conversionPattern);

    // Create Daily Rolling Log File Appender
    DailyRollingFileAppender rollingAppenderObj = new DailyRollingFileAppender();
    rollingAppenderObj.setFile("bin/OracleApp.log");
    rollingAppenderObj.setDatePattern("yyyy-MM-dd'.log'");
    rollingAppenderObj.setLayout(patternLayoutObj);
    rollingAppenderObj.activateOptions();

    // Configure the Root Logger
    Logger rootLoggerObj = Logger.getRootLogger();
    // rootLoggerObj.setLevel(Level.DEBUG);
    rootLoggerObj.addAppender(rollingAppenderObj);

    // Create a Customer Logger & Logs Messages
    Logger loggerObj = Logger.getLogger(OracleServices.class);

    switch (logType) {
        case "info":
            loggerObj.warn(logText);
            System.out.println("logged info");
            break;
        case "warn":
            loggerObj.warn(logText);
            System.out.println("logged warn");
            break;
        case "error":
            loggerObj.error(logText);
            System.out.println("logged error");
            break;
    } 

}

}
然后在另一个类中调用logerobj:

dynamicRollingLogFile.makeLogger("error", result);

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题