相同的log4j2 xml但具有不同的输出

时间:2018-06-18 13:29:18

标签: java log4j

在我上个学期,我参与了一个项目,现在我将从头开始重写,以改善我的错误并改进模型。

目前,我还没有得到我应该记录某些内容的习惯。我想将它输出到控制台并写入.log文件。

Log4j2.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorinterval="60" status="DEBUG" strict="true">

    <Properties>
        <Property name="filename">logs/gastro.log</Property>
    </Properties>

    <Appenders>
        <Appender type="Console" name="Console">
            <Layout type="PatternLayout" pattern="%d %p [%t] %m%n" />
        </Appender>

        <RollingFile name="RollingFile" fileName="${filename}"
                     filePattern="${log-path}/myexample-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <pattern>%d{dd.MM.yyyy HH:mm:ss:SSS}- %c{1}: %m%n</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="1000 KB" />
            </Policies>
            <DefaultRolloverStrategy max="4" />
        </RollingFile>
    </Appenders>

    <Loggers>
        <Logger name="root" level="debug" additivity="false">
            <appender-ref ref="Console" lefel="debug" />
            <appender-ref ref="RollingFile" level="debug" />
        </Logger>
        <Root level="debug" additivity="false">
            <AppenderRef ref="RollingFile" />
        </Root>
    </Loggers>
</Configuration>

控制台的输出:

Replaced in edit

我的测试课,我只是记录了一个tesmessage:

public class PersonDAOTest {

    PersonDAO personDAO = new PersonDAOImpl();

    private static Logger logger = LogManager.getLogger(PersonDAOTest.class);

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
        Util.deleteAllPerson();
    }

    public void init() throws Exception {
        Util.createPersonList();
    }

    @Test
    public final void testSave() throws Exception {
        init();
        assertTrue(personDAO.findAll().size() == Util.INIT_SIZE_PERSON);
    }

    @Test
    public final void findByUsername() throws Exception {
        init();
        assertTrue(personDAO.findByUsername("mmarco").getSurname().equals("Meier"));
        logger.info("Hello");
    }

    @Test
    public final void findBySurnameAndForename() throws Exception {
        init();
        assertTrue(personDAO.findBySurnameAndForename("Meier", "Marco").size() == 1);
    }

    @Test
    public final void findBySurname() throws Exception {
        init();
        assertTrue(personDAO.findBySurname("Meier").size() == 1);
    }

    @Test
    public final void findByForename() throws Exception {
        init();
        assertTrue(personDAO.findByForename("Marco").size() == 1);
    }

    @Test
    public final void updatePerson() throws Exception {
        init();
        Person person = personDAO.findAllPerson().get(0);
        Person personToCompareTo = personDAO.findAllPerson().get(0);
        person.setForename("TestName");
        personDAO.update(person);
        assertTrue(personDAO.findByForename("TestName").size() == 1);
    }

    @Test
    public final void deletePerson() throws Exception {
        init();
        int listSizeBeforeDelete = personDAO.findAllPerson().size();
        personDAO.delete(personDAO.findAllPerson().get(0));
        assertTrue(listSizeBeforeDelete != personDAO.findAllPerson().size());
    }
}

我希望控制台输出与写入文件的内容相同:

18.06.2018 15:19:51:786- PersonDAOTest: Hello

感谢您的帮助!

编辑:

配置状态=跟踪:

2018-06-18 16:05:28,496 main DEBUG Initializing configuration XmlConfiguration[location=C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml]
2018-06-18 16:05:28,504 main DEBUG Installed script engines
2018-06-18 16:05:28,813 main DEBUG Oracle Nashorn Version: 1.8.0_171, Language: ECMAScript, Threading: Not Thread Safe, Compile: true, Names: {nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript}
2018-06-18 16:05:28,814 main DEBUG PluginManager 'Core' found 107 plugins
2018-06-18 16:05:28,814 main DEBUG PluginManager 'Level' found 0 plugins
2018-06-18 16:05:28,821 main DEBUG 1 starting Log4j2 ConfigurationScheduler threads
2018-06-18 16:05:28,824 main DEBUG Building Plugin[name=property, class=org.apache.logging.log4j.core.config.Property].
2018-06-18 16:05:28,835 main TRACE TypeConverterRegistry initializing.
2018-06-18 16:05:28,836 main DEBUG PluginManager 'TypeConverter' found 23 plugins
2018-06-18 16:05:28,845 main DEBUG createProperty(name="filename", value="logs/gastro.log")
2018-06-18 16:05:28,845 main DEBUG Building Plugin[name=properties, class=org.apache.logging.log4j.core.config.PropertiesPlugin].
2018-06-18 16:05:28,849 main DEBUG configureSubstitutor(={filename=logs/gastro.log}, Configuration(C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml))
2018-06-18 16:05:28,850 main DEBUG PluginManager 'Lookup' found 13 plugins
2018-06-18 16:05:28,851 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2018-06-18 16:05:28,857 main DEBUG PatternLayout$Builder(pattern="%d %p [%t] %m%n", PatternSelector=null, Configuration(C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", noConsoleNoAnsi="null", header="null", footer="null")
2018-06-18 16:05:28,857 main DEBUG PluginManager 'Converter' found 41 plugins
2018-06-18 16:05:28,872 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
2018-06-18 16:05:28,880 main DEBUG ConsoleAppender$Builder(target="null", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d %p [%t] %m%n), name="Console", Filter=null)
2018-06-18 16:05:28,880 main DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2018-06-18 16:05:28,881 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
2018-06-18 16:05:28,881 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2018-06-18 16:05:28,882 main DEBUG PatternLayout$Builder(pattern="%d{dd.MM.yyyy HH:mm:ss:SSS}- %c{1}: %m%n", PatternSelector=null, Configuration(C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", noConsoleNoAnsi="null", header="null", footer="null")
2018-06-18 16:05:28,882 main DEBUG Building Plugin[name=SizeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy].
2018-06-18 16:05:28,884 main DEBUG createPolicy(size="1000 KB")
2018-06-18 16:05:28,885 main DEBUG Building Plugin[name=Policies, class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy].
2018-06-18 16:05:28,886 main DEBUG createPolicy(={SizeBasedTriggeringPolicy(size=1024000)})
2018-06-18 16:05:28,887 main DEBUG Building Plugin[name=DefaultRolloverStrategy, class=org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy].
2018-06-18 16:05:28,888 main DEBUG createStrategy(max="4", min="null", fileIndex="null", compressionLevel="null", ={}, stopCustomActionsOnError="true", Configuration(C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml))
2018-06-18 16:05:28,889 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.RollingFileAppender].
2018-06-18 16:05:28,892 main DEBUG RollingFileAppender$Builder(fileName="logs/gastro.log", filePattern="${log-path}/myexample-%d{yyyy-MM-dd}-%i.log", append="null", locking="null", Policies(CompositeTriggeringPolicy(policies=[SizeBasedTriggeringPolicy(size=1024000)])), DefaultRolloverStrategy(DefaultRolloverStrategy(min=1, max=4)), advertise="null", advertiseUri="null", createOnDemand="null", Configuration(C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml), bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d{dd.MM.yyyy HH:mm:ss:SSS}- %c{1}: %m%n), name="RollingFile", Filter=null)
2018-06-18 16:05:28,895 main TRACE New file 'logs/gastro.log' created = false
2018-06-18 16:05:28,895 main DEBUG Starting RollingFileManager logs/gastro.log
2018-06-18 16:05:28,896 main DEBUG PluginManager 'FileConverter' found 2 plugins
2018-06-18 16:05:28,897 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2018-06-18 16:05:28,898 main DEBUG createAppenders(={Console, RollingFile})
2018-06-18 16:05:28,898 main DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
2018-06-18 16:05:28,898 main ERROR appender-ref contains an invalid element or attribute "lefel"
2018-06-18 16:05:28,898 main DEBUG createAppenderRef(ref="Console", level="null", Filter=null)
2018-06-18 16:05:28,899 main DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
2018-06-18 16:05:28,899 main DEBUG createAppenderRef(ref="RollingFile", level="DEBUG", Filter=null)
2018-06-18 16:05:28,899 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
2018-06-18 16:05:28,900 main DEBUG createLogger(additivity="false", level="DEBUG", name="root", includeLocation="null", ={Console, RollingFile}, ={}, Configuration(C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml), Filter=null)
2018-06-18 16:05:28,903 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2018-06-18 16:05:28,904 main DEBUG createAppenderRef(ref="RollingFile", level="null", Filter=null)
2018-06-18 16:05:28,904 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
2018-06-18 16:05:28,905 main DEBUG createLogger(additivity="false", level="DEBUG", includeLocation="null", ={RollingFile}, ={}, Configuration(C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml), Filter=null)
2018-06-18 16:05:28,905 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
2018-06-18 16:05:28,906 main DEBUG createLoggers(={root, root})
2018-06-18 16:05:28,906 main DEBUG Configuration XmlConfiguration[location=C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml] initialized
2018-06-18 16:05:28,906 main DEBUG Starting configuration XmlConfiguration[location=C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml]
2018-06-18 16:05:28,908 main DEBUG Started configuration XmlConfiguration[location=C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml] OK.
2018-06-18 16:05:28,908 main TRACE Stopping org.apache.logging.log4j.core.config.DefaultConfiguration@2ac1fdc4...
2018-06-18 16:05:28,909 main TRACE DefaultConfiguration notified 1 ReliabilityStrategies that config will be stopped.
2018-06-18 16:05:28,909 main TRACE DefaultConfiguration stopping root LoggerConfig.
2018-06-18 16:05:28,909 main TRACE DefaultConfiguration notifying ReliabilityStrategies that appenders will be stopped.
2018-06-18 16:05:28,909 main TRACE DefaultConfiguration stopping remaining Appenders.
2018-06-18 16:05:28,909 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
2018-06-18 16:05:28,909 main DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
2018-06-18 16:05:28,910 main TRACE DefaultConfiguration stopped 1 remaining Appenders.
2018-06-18 16:05:28,910 main TRACE DefaultConfiguration cleaning Appenders from 1 LoggerConfigs.
2018-06-18 16:05:28,910 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@2ac1fdc4 OK
2018-06-18 16:05:28,961 main TRACE Reregistering MBeans after reconfigure. Selector=org.apache.logging.log4j.core.selector.ClassLoaderContextSelector@7dc0f706
2018-06-18 16:05:28,962 main TRACE Reregistering context (1/1): '18b4aac2' org.apache.logging.log4j.core.LoggerContext@4009e306
2018-06-18 16:05:28,963 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2'
2018-06-18 16:05:28,963 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=StatusLogger'
2018-06-18 16:05:28,963 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=ContextSelector'
2018-06-18 16:05:28,964 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=*'
2018-06-18 16:05:28,964 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=*'
2018-06-18 16:05:28,964 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=AsyncAppenders,name=*'
2018-06-18 16:05:28,964 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=AsyncLoggerRingBuffer'
2018-06-18 16:05:28,965 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=*,subtype=RingBuffer'
2018-06-18 16:05:28,966 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2
2018-06-18 16:05:28,969 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=StatusLogger
2018-06-18 16:05:28,971 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=ContextSelector
2018-06-18 16:05:28,972 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=
2018-06-18 16:05:28,974 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=Console
2018-06-18 16:05:28,974 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=RollingFile
2018-06-18 16:05:28,976 main TRACE Using default SystemClock for timestamps.
2018-06-18 16:05:28,976 main TRACE Using DummyNanoClock for nanosecond timestamps.
2018-06-18 16:05:28,976 main DEBUG Reconfiguration complete for context[name=18b4aac2] at URI C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml (org.apache.logging.log4j.core.LoggerContext@4009e306) with optional ClassLoader: null
2018-06-18 16:05:28,977 main DEBUG Shutdown hook enabled. Registering a new one.
2018-06-18 16:05:28,978 main DEBUG LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@4009e306] started OK.
[EL Info]: 2018-06-18 16:05:29.69--ServerSession(576894497)--EclipseLink, version: Eclipse Persistence Services - 2.7.0.v20170811-d680af5
[EL Info]: connection: 2018-06-18 16:05:29.891--ServerSession(576894497)--/file:/C:/Users/My Name/Documents/Dev/GastronomySoftware/gastronomysoftware-persister/target/classes/_JpaDemosPU login successful
2018-06-18 16:05:30,075 pool-1-thread-1 DEBUG Stopping LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@4009e306]
2018-06-18 16:05:30,075 pool-1-thread-1 DEBUG Stopping LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@4009e306]...
2018-06-18 16:05:30,075 pool-1-thread-1 TRACE Unregistering 1 MBeans: [org.apache.logging.log4j2:type=18b4aac2]
2018-06-18 16:05:30,076 pool-1-thread-1 TRACE Unregistering 1 MBeans: [org.apache.logging.log4j2:type=18b4aac2,component=StatusLogger]
2018-06-18 16:05:30,076 pool-1-thread-1 TRACE Unregistering 1 MBeans: [org.apache.logging.log4j2:type=18b4aac2,component=ContextSelector]
2018-06-18 16:05:30,076 pool-1-thread-1 TRACE Unregistering 1 MBeans: [org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=]
2018-06-18 16:05:30,077 pool-1-thread-1 TRACE Unregistering 2 MBeans: [org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=RollingFile, org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=Console]
2018-06-18 16:05:30,077 pool-1-thread-1 TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=AsyncAppenders,name=*'
2018-06-18 16:05:30,077 pool-1-thread-1 TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=AsyncLoggerRingBuffer'
2018-06-18 16:05:30,077 pool-1-thread-1 TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=*,subtype=RingBuffer'
2018-06-18 16:05:30,077 pool-1-thread-1 TRACE Stopping XmlConfiguration[location=C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml]...
2018-06-18 16:05:30,077 pool-1-thread-1 TRACE XmlConfiguration notified 2 ReliabilityStrategies that config will be stopped.
2018-06-18 16:05:30,077 pool-1-thread-1 TRACE XmlConfiguration stopping 1 LoggerConfigs.
2018-06-18 16:05:30,078 pool-1-thread-1 TRACE XmlConfiguration stopping root LoggerConfig.
2018-06-18 16:05:30,078 pool-1-thread-1 TRACE XmlConfiguration notifying ReliabilityStrategies that appenders will be stopped.
2018-06-18 16:05:30,078 pool-1-thread-1 TRACE XmlConfiguration stopping remaining Appenders.
2018-06-18 16:05:30,078 pool-1-thread-1 DEBUG Shutting down RollingFileManager logs/gastro.log
2018-06-18 16:05:30,079 pool-1-thread-1 DEBUG Shut down RollingFileManager logs/gastro.log, all resources released: true
2018-06-18 16:05:30,080 pool-1-thread-1 DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false
2018-06-18 16:05:30,080 pool-1-thread-1 DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false, all resources released: true
2018-06-18 16:05:30,080 pool-1-thread-1 TRACE XmlConfiguration stopped 2 remaining Appenders.
2018-06-18 16:05:30,080 pool-1-thread-1 TRACE XmlConfiguration cleaning Appenders from 2 LoggerConfigs.
2018-06-18 16:05:30,080 pool-1-thread-1 DEBUG Log4j2 ConfigurationScheduler shutting down threads in java.util.concurrent.ScheduledThreadPoolExecutor@41bf32e3[Running, pool size = 1, active threads = 0, queued tasks = 1, completed tasks = 0]
2018-06-18 16:05:30,081 pool-1-thread-1 DEBUG Stopped XmlConfiguration[location=C:\Users\My Name\Documents\Dev\GastronomySoftware\gastronomysoftware-persister\target\classes\log4j2.xml] OK
2018-06-18 16:05:30,081 pool-1-thread-1 DEBUG Stopped LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@4009e306]...

0 个答案:

没有答案