1

我有 MyLogger 类,其中包含:

public class MyLogger {

    private final static Logger logger = Logger.getLogger(MyLogger.class.getName());
    private static FileHandler fileHandler;
    private static String loggerFileName;

    public MyLogger() { 
    }

    public static void createMyLogger(String filename){
        loggerFileName = filename;
        try {
            File loggerFile = new File(filename);
            boolean fileExists = loggerFile.exists();
            if(fileExists){
                loggerFile.delete();
                File lockFile = new File(filename+".lck");
                if(lockFile.exists())
                    lockFile.delete();
            }
            fileHandler = new FileHandler(filename,true);

        } catch (IOException e) {
            e.printStackTrace();
        }
        logger.addHandler(fileHandler);
        SimpleFormatter simpleFormatter = new SimpleFormatter();
        fileHandler.setFormatter(simpleFormatter);
    }

    public static void log(String msg) {
        logger.log(Level.INFO, msg);
    }

    public static void log(Exception ex) {
        logger.log(Level.SEVERE, "Exception",ex);
    }


    public static String getLoggerFileName() {
        return loggerFileName;
    }


    public static void setLoggerFileName(String loggerFileName) {
        MyLogger.loggerFileName = loggerFileName;
    }

而且我正在线程中进一步执行,即当我启动第一个进程时,会创建记录器文件并记录日志,但是当我启动另一个进程时,会再次创建不同的线程并创建新的记录器文件,但由于静态方法和参考它混合了两个记录器文件中的两个进程日志......

当我为每个线程启动进程时,调用以下方法:

 public void start(String process) {
            try{
                String filename = process.replace(".com", "");
                MyLogger.createXPathLogger(filename.concat(WordUtils.capitalize(type))+ ".log");
                MyLogger.log("got parameters ===>> process : "+process);
                }catch (Exception e) {
                         MyLogger.log("Exception In main() method....");
                         MyLogger.log("*****"+process+" process failed In main() method.*****");
                         MyLogger.log(e);
              }
        }

那么我该怎么做呢,我可以做线程安全日志记录吗?提前致谢..

4

1 回答 1

0

这里的想法不会真的奏效。创建了一个记录器,然后将每个处理程序添加到记录器。

您真正需要的是日志上下文的概念。您需要在每个线程的基础上隔离记录器的创建,这是您无法使用 JUL 完成的 AFAIK

这是使用 JBoss 日志管理器执行此操作的一个非常粗略的示例:

import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.jboss.logmanager.LogContext;
public class MyLogger {

    private static final ThreadLocal<MyLogger> LOCAL_LOGGER = new ThreadLocal<MyLogger>();

    private final Logger logger;

    private MyLogger(final Logger logger) {
        this.logger = logger;
    }

    public static MyLogger createMyLogger(final String filename) {
        MyLogger result = LOCAL_LOGGER.get();
        if (result == null) {
            final LogContext logContext = LogContext.create();
            final Logger logger = logContext.getLogger(MyLogger.class.getName());
            final FileHandler fileHandler;
            try {
                final File loggerFile = new File(filename);
                if (loggerFile.exists()) {
                    loggerFile.delete();
                    File lockFile = new File(filename + ".lck");
                    if (lockFile.exists())
                        lockFile.delete();
                }
                fileHandler = new FileHandler(filename, true);

            } catch (IOException e) {
                throw new RuntimeException("Could not create file handler", e);
            }
            logger.addHandler(fileHandler);
            fileHandler.setFormatter(new SimpleFormatter());
            final MyLogger myLogger = new MyLogger(logger);
            LOCAL_LOGGER.set(myLogger);
            result = myLogger;
        }
        return result;
    }

    public void log(String msg) {
        logger.log(Level.INFO, msg);
    }

    public void log(Exception ex) {
        logger.log(Level.SEVERE, "Exception", ex);
    }
}
于 2012-10-17T18:56:37.710 回答