Java login

I create a registrar 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");
    }
}

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

  • Both System.err and File
  • Neither System.err nor the file
  • Just System.err
  • File only

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

EDIT: Thanks for the answers. I solved this 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");
        }
    }

thanks for the help

+3
source share
3 answers

, ; .

Handler, handler.setLevel(...);

, . , , , , .

+2

. API : Logger (Java Platform SE 6).

removeHandler (Handler), , , , FileHandler.

. , , . , .

+1

, . : , ​​java .

  • jre/lib ( jre\lib windows) "logging.properties". , .

  • "handlers =" TO

    -For Both System.err File

    "handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler"
    

    -For System.err,

    Simply comment that line by adding # in the beginning of the line.
    

    -For Just System.err

    "handlers = java.util.logging.ConsoleHandler"
    

    - Just File

    "handlers = java.util.logging.FileHandler"
    

Done! You can go and start registering :)

0
source

Source: https://habr.com/ru/post/1779278/


All Articles