java.util.logging将记录器分配给特定的包

时间:2015-08-10 21:29:32

标签: java logging java.util.logging

我实现了两个自定义处理程序来记录有关DB的信息和另一个平面文件(DBHandler和MyFileHandler)。这个新的日志处理程序将由特定包上的单个类使用。 我将两个新的记录器仅附加到特定的包中。 我的想法是在特定包上的类包含这两个处理程序(文件和数据库)之间切换,但是目前使用当前配置我无法做到这一点,所以要么我用两个处理程序都记录,要么根本没有记录。 我尝试将数据库处理程序的日志级别设置为关闭,但它仍然正常记录在数据库上。

配置文件下面的

是使用logging.properties

############################################################
#####   Global properties
############################################################

handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler, com.test.logging.DBHandler, com.test.logging.MyFileHandler

.level = INFO

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

    java.util.logging.FileHandler.level = ALL
    java.util.logging.FileHandler.pattern = %t/CLog%g.log
    java.util.logging.FileHandler.limit = 50000
    java.util.logging.FileHandler.count = 1
    java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

    java.util.logging.ConsoleHandler.level = ALL
    java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

    com.test.logging.MyFileHandler.level = ALL
    com.test.logging.MyFileHandler.pattern = %t/custLog%g.log
    com.test.logging.MyFileHandler.limit = 50000
    com.test.logging.MyFileHandler.count = 1
    com.test.logging.MyFileHandler.formatter = java.util.logging.SimpleFormatter

    com.test.logging.DBHandler.level=OFF

    com.test.ccb.mon.handlers=com.test.logging.DBHandler, com.test.logging.MyFileHandler

使用记录器跟踪他的信息的班级在

之下
package com.test.ccb.mon;
    public class Utils {

    public static final Logger logger = Logger.getLogger(Utils.class.getCanonicalName());

public void logging()
{

 //processing
   logger.info("message);

}

}

DBHandler类:

public class DBHandler extends Handler {

    @Override
    public void close() throws SecurityException {

    }

    @Override
    public void flush() {


    }

    @Override
    public void publish(LogRecord logRecord) {
        if (isLoggable(logRecord))
        {

            try {
                      //SQL call to insert onDB
            } catch (Exception e) {
                e.printStackTrace();
            } 
        }

    }

}

MyFileHandler类:

public class MyFileHandler extends FileHandler{

        public MyileHandler() throws IOException, SecurityException {
            super();
        }

        @Override
        public void close() throws SecurityException {
            super.close();

        }

        @Override
        public void flush() {
            super.flush();

        }

        @Override
        public void publish(LogRecord record) {
            super.publish(record);
        }

    }

2 个答案:

答案 0 :(得分:0)

对我来说,重现你的问题并不容易。对于与您的类似的处理程序类,对配置文件的更改具有预期的效果。使用DBHandler.level=OFF设置,我缺少数据库处理程序输出:

Aug 11, 2015 1:47:26 PM com.test.ccb.mon.Utils logging
DBHandler.publish - handler level: OFF; log record level: INFO
INFO: message
MyFileHandler - message

Logging handlers:
###java.util.logging.FileHandler-ALL
###java.util.logging.ConsoleHandler-ALL
###com.test.logging.DBHandler-OFF
###com.test.logging.MyFileHandler-ALL

用于打印日志处理程序的调试代码现在也包含在main类的Utils方法中。您可以自己运行此方法,以查看这种读取配置文件的方式是否适合您:

public static void main(final String[] arguments) throws IOException
{
    final String fileName = "logging.properties";
    final InputStream propertiesStream = Utils.class.getResourceAsStream(fileName);
    //final InputStream propertiesStream = new FileInputStream("path to file");
    LogManager.getLogManager().readConfiguration(propertiesStream);

    new Utils().logging();
    System.out.println();

    // No handlers for this logger directly, but four for its parent.
    System.out.println("Logging handlers:");
    for (final Handler handler : logger.getParent().getHandlers())
        System.out.println("###" + handler.getClass().getName()
                           + "-" + handler.getLevel());
}

DBHandler类的一个非常简单的版本可能如下所示(请注意if (isLoggable(record))方法中的publish检查:

package com.test.logging;

import java.util.logging.*;

/**
 * Logging handler that stores logging in the database.
 */
public class DBHandler extends Handler {
    @Override
    public void publish(final LogRecord record) {
        System.out.println("DBHandler.publish - handler level: " + getLevel()
                           + "; log record level: " + record.getLevel());
        if (isLoggable(record))
            System.out.println(getClass().getSimpleName() + " - " + record.getMessage());
    }

    @Override
    public void flush() {
        // Empty.
    }

    @Override
    public void close() throws SecurityException {
        // Empty.
    }
}

答案 1 :(得分:0)

默认情况下,Handler类不会从LogManager读取任何属性。您必须在所有子类中编写该逻辑。

#!/bin/bash

#User who's files are being backed up
BNAME=username
#directory to back up
BDIR=/home/username/BackThisUp
#directory to backup to
BackupDir=/var/home/username_local/BackupTo
#user
RUSER=$USER
#SSH Key
KEY=/var/home/username_local/.ssh
#Backupname
RBackup=`date +%F`
#Backup Server
BServ=backup.server
#Path
LPATH='Data for backup'
#date
DATE=`date +%F`

#make parent directory for backup
mkdir $BackupDir/$BNAME > /dev/null 2>&1

#Transfer new backups
rsync -avpHrz  -e "ssh -i $KEY" $BNAME@$BServ:$BDIR $BackupDir/$BNAME/$DATE


find $BackupDir/$BNAME -type d -ctime +30 -exec rm -rf {} \;