我的数据库中有一个日志表,用于跟踪登录、注销、超时以及用户在登录时所做的事情。我的 Java 设置为每次执行 LOGGER.info(message) 时,消息都会进入我的表的 MESSAGE 列。
这是 log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<appender name="db" class="org.apache.log4j.jdbcplus.JDBCAppender">
<param name="connector" value="com.blahblahblah.jdbcplus.MySqlConnectionHandler" />
<param name="sql"
value="INSERT INTO LOG(LEVEL, MESSAGE, TIMESTAMP, HOST) VALUES ('@PRIO@', '@MSG@', '@TIMESTAMP@', 'Test')" />
<layout class="org.apache.log4j.PatternLayout"></layout>
</appender>
<!-- Application Loggers -->
<logger name="com.blahblahblah.service.ZebraService">
<level value="info" />
<appender-ref ref="db" />
</logger>
<logger name="com.blahblahblah.service.ZplTemplateService">
<level value="info" />
<appender-ref ref="db" />
</logger>
<logger name="com.blahblahblah.service.MyService">
<level value="info" />
<appender-ref ref="db" />
</logger>
<logger name="com.blahblahblah.web.controller.ViewController">
<level value="info" />
<appender-ref ref="db" />
</logger>
<logger name="com.blahblahblah.security">
<level value="info" />
<appender-ref ref="db" />
</logger>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="error" />
</logger>
<logger name="org.springframework.beans">
<level value="error" />
</logger>
<logger name="org.springframework.context">
<level value="error" />
</logger>
<logger name="org.springframework.web">
<level value="error" />
</logger>
<logger name="org.springframework.web.servlet.DispatcherServlet">
<level value="error" />
</logger>
<logger name="org.springframework.security">
<level value="error" />
</logger>
<logger name="org.springframework.aop">
<level value="error" />
</logger>
<logger name="org.springframework.ui">
<level value="error" />
</logger>
<!-- Root Logger -->
<root >
<priority value="error" />
<appender-ref ref="console" />
</root>
这会在 LEVEL 列中打印“INFO”,在 MESSAGE 列中打印有关登录/注销的信息,在 TIMESTAMP 列中打印时间戳,在 HOST 列中打印“Test”。
我想让 HOST 列成为本地机器名称。例如,我的计算机名称是 PC-1050,所以每次我在那台计算机上做某事时,我希望 HOST 说“PC-1050”。
我不知道如何执行@MSG@、@TIMESTAMP@ 等。我只是按照我被告知要做的事情去做。任何人都可以提供一些帮助吗?谢谢。