I am specifying the filename to log4j2 logger in the XML as below. All works fine but i get the error
2017-09-06 16:54:33,496 main ERROR Unknown object "RandomAccessFile" of type org.apache.logging.log4j.core.appender.RandomAccessFileAppender is ignored: try nesting it inside one of: ["Appenders", "Loggers", "Properties", "Scripts", "CustomLevels"].
<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="log4j.test"
status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd/MM/yyyy HH:mm:ss} %c %m%n"/>
</Console>
</Appenders>
<RandomAccessFile name="FILE" fileName="${sys:log4j.saveDirectory}/CMSAutomation.${date:yyyy-MM-dd_hh-mm-ss}.log" append="true" immediateFlush="false">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-6p %C{1}.%t:%L >>> %m%n"/>
</RandomAccessFile >
<Loggers>
<Logger name="log4j.test.Log4jTest" level="debug">
<AppenderRef ref="Console"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
This error is coming because RandomAccessFile appender is not defined within <Appenders> XML element.
Below is the correct way of defining any appender including RandomAccessFile appender -
<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="log4j.test"
status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd/MM/yyyy HH:mm:ss} %c %m%n"/>
</Console>
<RandomAccessFile name="FILE" fileName="${sys:log4j.saveDirectory}/CMSAutomation.${date:yyyy-MM-dd_hh-mm-ss}.log" append="true" immediateFlush="false">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-6p %C{1}.%t:%L >>> %m%n"/>
</RandomAccessFile>
</Appenders>
<Loggers>
<Logger name="log4j.test.Log4jTest" level="debug">
<AppenderRef ref="Console"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
You can check the same in log4j2 documentation here.
Additional one point, you are not using RandomAccessFile appender in any logger. If you will not define it in any logger, it wouldn't work.
Related
I try to config log4j to appends loggs in diffrent color per log type (INFO, ERROR, DEBUG etc.)
Here is my log4j2.xml:
<Configuration monitorInterval="60">
<Properties>
<Property name="log-path">applogs</Property>
</Properties>
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
%highlight{%d{HH:mm:ss.SSS} %-5level %logger{36}.%M() [%L] - %msg%n}{FATAL=red, ERROR=red, WARN=yellow, INFO=green}
</pattern>
</PatternLayout>
</Console>
<someMoreAppenders>
</Appenders>
<Loggers>
<Logger name="RepositoriesController" level="info" additivity="false">
<AppenderRef ref="Console-Appender"/>
</Logger>
<Root level="error" additivity="false">
<AppenderRef ref="Console-Appender"/>
</Root>
</Loggers>
</Configuration>
And in class RepositoriesController, i create logger in this way:
private static final Logger logger = LogManager.getLogger("RepositoriesController");
logger.info("Hello, World!");
My log4j.properties are empty. Output seems to be formatted used by my pattern but font color is still white:
Intelij console
i tried to add this properties:
spring.main.banner-mode=off
spring.output.ansi.enabled=ALWAYS
and modify pattern but without succesed, any advices?
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t]
%highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue} - %msg%n" />
</Console>
%highlight{%level} - using this you can highlight colors for different levels of logs.
This will generate output as:
I am using log4j2 version 2.7 with sprint-boot 1.5:
my configuration file (log4j2.xml) is:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="60">
<Properties>
<Property name="LOG_PATH">./log</Property>
<Property name="FILE_LOG_PATTERN">%date{yyyy-MM-dd'T'HH:mm:ss.SSSZ}{UTC}}, %5p, %X{component:-SERVICE}, %X{event:-EVENT}, ${PID:-???}, [%t], %40.40logger{40}, FCID=%X{FCID:-N/A}, %msg%n</Property>
<Property name="MAX_HISTORY">7</Property>
<Property name="MAX_FILE_SIZE">100MB</Property>
</Properties>
<Appenders>
<RollingFile name="FILE"
fileName="${LOG_PATH}/tests.log"
filePattern="${LOG_PATH}/tests.%d{yyyy-MM-dd}.%i.log.gz">
<PatternLayout>
<Pattern>${FILE_LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="${MAX_FILE_SIZE}"/>
</Policies>
<DefaultRolloverStrategy max="${MAX_HISTORY}"/>
</RollingFile>
</Appenders>
<Loggers>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework.security.oauth2" level="INFO"/>
<logger name="org.springframework.http" level="ERROR"/>
<Logger name="org.springframework.boot" level="info"/>
<Logger name="org.springframework.boot.context.embedded.jetty" level="info"/>
<Root level="error">
<AppenderRef ref="FILE"/>
</Root>
</Loggers>
when I start the application I get this error:
ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile. java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender
If I remove one of the policies this error not showing. What is the problem?
How do I print just the INFO levels?
My current log is printing all INFO, WARN, ERROR and FATAL levels to console - I understand why this is happening. (because they are all above INFO)
Is there a way to just specify that just and only just, INFO levels get sent to the console appender and not everything above it?
Currently have the following log4j2 xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="App" packages="">
<Appenders>
<File name="logFile" fileName="C:\Users\user\workspace\logs\log.out">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="com.App" level="info" additivity="false">
<AppenderRef ref="logFile" level="error"/>
<AppenderRef ref="Console" level="info"/>
</Logger>
<Root level="error">
<AppenderRef ref="logFile" />
</Root>
</Loggers>
Need to use a ThresholdFilter;
<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="ACCEPT" />
This blocks the WARN level and everything above it. So now it only accepts info.
I've been using log4j 2 (2.7) and it worked very well until I had to reinstall the IDE (Eclipse Neon J2EE edition).
Now I only works loggingto a log file but not to the console.
This is my log4j2.xml file:
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="log" fileName="target/test.log" append="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M %msg%xEx%n"/>
</File>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
<Root level="TRACE">
<AppenderRef ref="log" />
</Root>
</Loggers>
The output to the log works perfectly, as I said.
Try declaring Root only once.
Example:
<Loggers>
<Root>
<AppenderRef ref="Console" level="error"/>
<AppenderRef ref="log" level="trace"/>
</Root>
</Loggers>
I agree with Sazzad 's answer: you can only have one root logger. I would suggest specifying a level on the root logger and override the level only on the AppenderRef that need it. So:
<Loggers>
<Root level="trace">
<AppenderRef ref="Console" level="error"/>
<AppenderRef ref="log"/>
</Root>
</Loggers>
Also, not sure if this was omitted just from the question or from the actual configuration file, but the configuration really should start with a <Configuration> and end with a </Configuration> element.
in the config file how to separate the package in different appaender using asynch
i have two different package and using different appender i want separate log output
first two appender is different package and third appender is different one
and i need all appender connect to asynch...
pls help me to solve this
here my config file..
<?xml version="1.0" encoding="UTF-8"?>
<configuration strict="true" status="WARN">
<appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</Console>
<File name="MyFile" fileName="Logger/knight.log">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</File>
<File name="Except" fileName="log1/Execption9.log">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</File>
<Async name="Async">
<appender-ref ref="CONSOLE" />
<appender-ref ref="MyFile" />
<appender-ref ref="Except" />
</Async>
</appenders>
<loggers>
<root level="all">
<appender-ref ref="Async" />
</root>
</loggers>
</configuration>
You could configure three separate AsyncAppenders, one for each of the File appenders, and one for console, then configure root as:
<root>
<appender-ref ref="async-console" level="info" />
<appender-ref ref="async-file1" level="trace" />
<appender-ref ref="async-file2" level="error" />
</root>
But this solution gives you three threads. Why not use AsyncLoggers (using the selector command line option, see the docs for AsyncLoggers)?
That way you only have one logging thread, and it is probably a lot faster too...