标签: java.util.logging

在记录活动中使用 readConfiguration 方法

为了在小型 Java 桌面应用程序中使用日志记录,我试图深入了解某些方法的操作。我使用一个非常愚蠢的小型 Java 程序来测试它们。

特别是,在测试 LogManager.readConfiguration() 方法的行为时,我发现了一些奇怪的东西。在所有测试中,LogManager 从位于 JRE 目录中 lib/logging.properties 的属性文件中读取其配置。这时候这个文件的内容如下:

handlers=java.util.logging.ConsoleHandler
myapp2.handlers=java.util.logging.ConsoleHandler
myapp2.MyApp2.handlers=java.util.logging.ConsoleHandler
.level=OFF
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
myapp2.level=WARNING
myapp2.MyApp2.level=INFO
Run Code Online (Sandbox Code Playgroud)

java程序的代码是:

package myapp2;

import java.io.IOException;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class MyApp2 {

    private static final Logger LOGGER = Logger.getLogger(MyApp2.class.getPackage().getName());
    private static final Logger LOGGER1 = Logger.getLogger(MyApp2.class.getName());

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        LOGGER.severe("severe at MyApp2");
        LOGGER.warning("warning at MyApp2");
        LOGGER.info("info at MyApp2");
        LOGGER1.severe("severe1 at MyApp2");
        LOGGER1.warning("warning1 at …
Run Code Online (Sandbox Code Playgroud)

java java.util.logging

2
推荐指数
1
解决办法
5538
查看次数

Java 日志记录 - 有没有办法控制将哪些数据插入到 xml 中?

我正在学习 java 日志记录机制,并编写了以下代码。以下代码将 2 条消息记录在名为LOGGED.xml.

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

class Tester {
  public static void main(String args[]) throws IOException{
    Logger logger = Logger.getLogger(Tester.class.getName());
    FileHandler fHandler = new FileHandler("LOGGED.xml");
    logger.addHandler(fHandler);
    logger.log(Level.INFO,"This is an info log message");
    logger.log(Level.WARNING,"This is a warning message");
    fHandler.close();
  }
}
Run Code Online (Sandbox Code Playgroud)

以下是我在xml文件中看到的内容:

<log>
 <record>
  <date>2013-02-02T16:42:48</date>
  <millis>1359803568992</millis>
  <sequence>0</sequence>
  <logger>Tester</logger>
  <level>INFO</level>
  <class>Tester</class>
  <method>main</method>
  <thread>1</thread>
  <message>This is an info log message</message>
 </record>

 <record>
  <date>2013-02-02T16:42:49</date>
  <millis>1359803569031</millis>
  <sequence>1</sequence>
  <logger>Tester</logger>
  <level>WARNING</level>
  <class>Tester</class>
  <method>main</method>
  <thread>1</thread>
  <message>This is …
Run Code Online (Sandbox Code Playgroud)

java logging java.util.logging

1
推荐指数
1
解决办法
3365
查看次数

如果没有 basicConfig,日志处理程序将无法工作

我有一个 Flask Web 应用程序,Python 日志记录配置是在应用程序启动时通过dictConfig完成的。用于将某些日志写入数据库的处理程序附加到 logger 'test.module' 对该记录器生成的日志仅logging.basicConfig(level=logging.DEBUG)在应用程序启动时也被调用时才会写入数据库。否则不会将日志写入数据库。我知道 basicConfig 只是将一个 streamHandler 附加到根记录器。我认为这应该无关紧要,因为我不希望 root 记录器做任何事情。为什么没有 basicConfig 这不起作用?

我在下面添加了我如何启动记录器和我的配置。

class DbHandler(logging.Handler):
    def __init__(self, level=logging.NOTSET):
        logging.Handler.__init__(self, level)

    def emit(self, record):
        record.message = self.format(record)
        log = DbModel()
        log.message = record.message
        log.save()


LOGGING = {
    'version': 1,
    'handlers': {
        'db_log': {
            'level': 'DEBUG',
            'class': 'test.handlers.DbHandler',
        },
    },
    'loggers': {
        'test.important_module': {
            'handlers': [
                'db_log'
            ],
    },
}

# logging.basicConfig(level=logging.DEBUG) # Doesnt work without this
logging.config.dictConfig(LOGGING)

logger = logging.getLogger('test.important_module')
logger.info('Making a test')
Run Code Online (Sandbox Code Playgroud)

python logging java.util.logging

1
推荐指数
1
解决办法
2712
查看次数

如何格式化java日志中的日期?

我在用 :

log.info("Message");
Run Code Online (Sandbox Code Playgroud)

输出是:

Jul 8, 2014 12:58:02 PM myclass main
INFO: message
Run Code Online (Sandbox Code Playgroud)

我如何只打印没有日期和信息的消息:?

java java.util.logging

1
推荐指数
1
解决办法
5729
查看次数

将处理程序添加到每个记录器

我想为我的项目的每个类的每个记录器添加一个自定义处理程序。我有一个 logging.properties 文件,它在最开始时使用:

try (InputStream in = ReportingService.class.getResourceAsStream("logging.properties")) {
    LogManager.getLogManager().readConfiguration(in);
} catch (IOException ex) {
    Logger.getLogger(myClass.class.getName()).log(Level.SEVERE, null, ex);
}
Run Code Online (Sandbox Code Playgroud)

logging.properties 文件如下所示:

handlers=java.util.logging.ConsoleHandler,myPackage.AlertHandler
.level=SEVERE
java.util.logging.ConsoleHandler.level=SEVERE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
myPackage.AlertHandler.level=SEVERE
myPackage.AlertHandler.formatter=java.util.logging.SimpleFormatter
Run Code Online (Sandbox Code Playgroud)

而 myPackage.AlertHandler.java 看起来像这样:

package myPackage;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import javafx.scene.control.Alert;
public class AlertHandler extends Handler {
    @Override
    public void publish(LogRecord lr) {
        Alert a = new Alert(Alert.AlertType.ERROR);
        a.setTitle("Exception!");
        a.setHeaderText("Exception was thrown, here is the StackTrace:");
        a.setContentText(getFormatter().formatMessage(lr));
        Platform.runLater(()->{a.showAndWait();});
    }
    @Override
    public void flush() {
        //no real handler is open, nothing to flush
    } …
Run Code Online (Sandbox Code Playgroud)

java logging java.util.logging java-8

1
推荐指数
1
解决办法
5845
查看次数

java.util.logging.SimpleFormatter.format毫秒

我正在使用JUL进行日志记录,我有一个属性文件.我想在日志文件中以毫秒为单位记录日期时间.

这是我的财产:

java.util.logging.SimpleFormatter.format=%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%n
Run Code Online (Sandbox Code Playgroud)

以上记录如下:

Dec 05, 2016 5:08:06 PM ...
Run Code Online (Sandbox Code Playgroud)

我怎么能显示毫秒?

java logging date java.util.logging

1
推荐指数
1
解决办法
5591
查看次数

性能和功能log4j和Logging Util类之间的区别

我是Java.util包中的日志类的新手,我已经开发了一个应用程序,并希望在不同的级别实现日志记录机制,我只想到哪一个去找出它们之间的差异2.我正在寻找1)记录器应易于维护,可实现.任何人都可以对此提出建议.

java logging error-logging log4j java.util.logging

0
推荐指数
1
解决办法
1531
查看次数

Java java.util.logging记录器与HSQLDB结合的奇怪行为

情况:我有一个静态数据库类,使用HSQLDB,我也想在其中使用Logging.

我设置了记录器,设置了所有语句,但在使用特定方法后,记录器似乎不再起作用了.没有例外被抛出,似乎已经死了.

这是Logger类:

public class GLogger {

    private static GFormatter   formatterHTML;
    private static FileHandler  fileHTML;
    private static boolean      isReady = false;

public static void setup() throws IOException {
    Logger logger = Logger.getLogger("");
    fileHTML = new FileHandler("conf/logging.html");
    formatterHTML = new GFormatter();
    fileHTML.setFormatter(formatterHTML);
    logger.addHandler(fileHTML);
    isReady = true;
}

public static boolean isReady() {
    return isReady;
}
}
Run Code Online (Sandbox Code Playgroud)

这是数据库类的(部分):

public class Database {

private static final Logger LOG = Logger.getLogger(Database.class.getName());
/**
 * The database server class
 */
private static Server       hsqlServer;

/**
 * …
Run Code Online (Sandbox Code Playgroud)

java hsqldb java.util.logging

0
推荐指数
1
解决办法
372
查看次数