登录Java [英] Logging in Java

查看:128
本文介绍了登录Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过执行以下代码在java中创建一个记录器:

I am creating a logger in java by executing the following code:



private static final String logFile = "." + File.separator + "Log Files" + File.separator + "Log_" + Long.toString(System.currentTimeMillis());
private static Logger logger = Logger.getLogger("JCS_Logger");
static
{
    try
    {
        logger.addHandler(new FileHandler(logFile ));
    }
    catch (Exception e)
    {
       System.err.println("Could not return a static logger");
    }
}

如果我使用此记录器,它不仅写入System.err,还写入文件。最终我想配置记录器以允许它写入:'

If I use this logger, it writes to not only System.err, but also to the file. Ultimately I want to configure the logger to allow it to write to:'


  1. System.err和File

  2. System.err和File

  3. Just System.err

  4. Just File

  1. Both System.err and File
  2. Neither System.err and File
  3. Just System.err
  4. Just File

我知道调用 logger.setLevel(Level.OFF)将关闭所有日志记录,但我不知道如何为上面的列表做这个?它是通过Level类完成的吗?任何想法或建议将不胜感激。

I know that calling logger.setLevel(Level.OFF) will turn off all logging, but I am not sure about how to do it for the list above? Is it done through the Level class? Any ideas or suggestions would be greatly appreciated.

编辑:
感谢您的回复。我用这段代码解决了这个问题:

Thanks for the responses. I solved it with this code:



    /**
     * If debug should be turned on
     */
    static boolean debug = true;
    /**
     * If writing debug to file
     */
    static boolean writeLogToFile = false;
    /**
     * If writing debug to System.err
     */
    static boolean writeLogToStdErr = true;

    /**
     * Location for the temp file
     */
    private static final String logFile = "." + File.separator + "Log Files" + File.separator + "Log_" + Long.toString(System.currentTimeMillis());
    /**
     * Instance of the logger
     */
    private static Logger logger = Logger.getLogger("JCS_Logger");
    /**
     * Handler for System.err
     */
    private static Handler consoleHandler;
    /**
     * Handler for the log file
     */
    private static Handler fileHandler;

    static
    {
        try
        {   //if not debuggin at all          
            if(debug == false)
            {
                logger.setLevel(Level.OFF);
            }
            //if not writing to the file
            if(writeLogToFile == true)
            {
                fileHandler = new FileHandler(BCApp.getLogFileHandlerPath());
                logger.addHandler(fileHandler);
            }
            //if not writing to System.err
            if(writeLogToStdErr == false)
            {
                consoleHandler = logger.getParent().getHandlers()[0];
                logger.getParent().removeHandler(consoleHandler);
            }
        }
        catch (Exception e)
        {
            System.err.println("Could not return a static logger");
        }
    }

感谢您的帮助

推荐答案

您可以只为想要的项目添加处理程序,在登录启动时进行选择;或者,您可以添加所有处理程序,并通过记录阈值调整每个处理程序。

You can either add handlers only for the wanted items, making your selection at logging start-up time; or, you can add all the handlers, and tune each Handler via logging thresholds.

要调整处理程序的记录阈值,请使用 handler.setLevel( ...);

To tune a Handler's logging threshold, use handler.setLevel(...);

请注意,在启动时,已配置默认处理程序。您可能需要以编程方式删除该处理程序,或者将其调整为不处理任何内容,具体取决于您希望实现的日志处理程序处理技术。

Note that on start-up a default Handler is already configured. You might need to programmatically remove that handler, or tune it to handle nothing depending on which technique of log handler "handling" you wish to implement.

这篇关于登录Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆