如何在线程安全执行中编写util.logger?

时间:2012-10-17 06:17:28

标签: java logging thread-safety java.util.logging filehandler

我有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;
    }

我正在线程中进一步执行,即当我启动第一个进程时,会创建Logger文件并记录日志,但是当我启动另一个进程时,会再创建不同的线程并创建新的记录器文件但是因为静态方法和引用它将两个进程日志混合在两个记录器文件中......

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

 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);
              }
        }

那么我能为此做些什么,我可以做线程安全日志记录吗? 提前谢谢..

1 个答案:

答案 0 :(得分:0)

这里的想法不会真正起作用。创建一个记录器,然后将每个处理程序添加到记录器中。

您真正需要的是日志上下文的概念。你需要在每个线程的基础上隔离记录器的创建,而你不能用J.U.L来做AFAIK。

使用JBoss Log Manager执行此操作的边缘示例非常粗略:

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);
    }
}