log4j 2 - collor logs for intelij console - log4j2

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:

Related

Adding a filename to log4j2 logger

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.

Log4j2 - Seperate logs for different levels - How do I print just the INFO levels?

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.

Log4j2 RollingFile appenders clashing

Log4j2 RollingFile appenders clashing
Below is a simplified debug version of our log4j2 configuration file (We rollover nightly not every minute!).This configuration, instead of it creating a Rollover file each minute (as per theTimeBasedTriggeringPolicy) will create one rollover file (non-tarred), containing JSON formatted logging, which will be overwritten every 20KB(although it will end up being slightly greater than 20KB (See Screenshot).We also get the following errors (abbreviated with "..."):-
2016-10-07 08:47:34,433 default-workqueue-4 ERROR Unable to copy file /.../logs/logFile-2016-10-07-08:47:11.log to /.../logs/logFile-2016-10-07-08:47:11.log: java.nio.file.NoSuchFileException /.../logs/logFile-2016-10-07-08:47:11.log
If we switch the order of the timeBasedRollingFileJsonLayout appender and the sizeBasedRollingFilePatternLayoutWithZippedArchive appender then no rollover occurs at all.
If we remove the sizeBasedRollingFilePatternLayoutWithZippedArchive appender then the timeBasedRollingFileJsonLayout appender works as expected.
We have the two different appenders for different environments, where the logs may or may not be hooked up to ELK.In our real log4j2 config file we use properties to select the appropriate appender for the environment.I have removed the properties from this file for clarity and to rule them out as a possible cause of the problem.
We are using log4j 2.6.2.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %logger{36} - %msg %n" />
</Console>
<RollingFile name="timeBasedRollingFileJsonLayout" append="true" fileName="logs/logFile.log" filePattern="logs/logFile-%d{yyyy-MM-dd-HH:mm}.log">
<JSONLayout properties="true" compact="true" eventEol="true" />
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
<RollingFile name="sizeBasedRollingFilePatternLayoutWithZippedArchive" append="true" fileName="logs/logFile.log" filePattern="logs/logFile-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %logger{36} - %msg %n" />
<Policies>
<SizeBasedTriggeringPolicy size="20KB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<logger name="logger.one" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</logger>
<Logger name="logger.two" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</Logger>
<Logger name="logger.three" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</Logger>
<Root level="info">
<AppenderRef ref="timeBasedRollingFileJsonLayout" level="all" />
</Root>
</Loggers>
</Configuration>
I am at a loss to understand why you think this should work. You have two appenders trying to write to the same file trying to rollover based on different criteria and rollover to files with different names. It is no surprise that you are getting the file is in use error since two things have it open at once.

Two patterns in same log file is not working in log4j2

I'd like to configure log4j 2 with two different patterns in same appender. i.e., Whenever there is an error, a specific pattern should be present in the log file. I am not trying two different log files, but two different pattern in same log file. Whenever there is an error, I would see "MYDOMAINDOTCOM_SUPPORT_NEEDED" and this string will trigger an automatic email to support team.
I have the below configuration which prints error message in "RollingFile" appender only and "RollingFileError" appender is ignored. What am I missing ?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="log-path">/documents/log</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${log-path}/myexample.log"
filePattern="${log-path}/$${date:yyyy-MM}/myexample-%d{yyyy-MM-dd}-%i.log"
immediateFlush="true">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %X{packetRefId} - %msg%n</pattern>
<!-- %d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n -->
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 KB" />
</Policies>
<DefaultRolloverStrategy max="4" />
</RollingFile>
<RollingFile name="RollingFileError" fileName="${log-path}/myexample.log"
filePattern="${log-path}/$${date:yyyy-MM}/myexample-%d{yyyy-MM-dd}-%i.log"
immediateFlush="true">
<param name="threshold" value="error" />
<PatternLayout>
<pattern>MYDOMAINDOTCOM_SUPPORT_NEEDED %d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %X{packetRefId} - %msg%n</pattern>
<!-- %d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n -->
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 KB" />
</Policies>
<DefaultRolloverStrategy max="4" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework.beans.factory" level="info" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug" />
<appender-ref ref="RollingFileError" level="error" />
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
You should never configure two appenders to write to the same file. Having two rolling file appenders that both use the same file and roll over to the same file pattern is never going to work correctly.
Also, your configuration would end up with all error messages being logged twice; once with the RollingFile appender due to its debug level, and once on the RollingFileError appender due to its error level.
Instead, you should have a single rolling file appender and use a PatternSelector to decide which pattern to use. See http://logging.apache.org/log4j/2.x/manual/layouts.html#Pattern_Selectors for documentation on pattern selectors.

log4j2 Logs not writing to file when running as jar

I have configured log4j2.xml within eclipse and all logs write correctly to a file.
When I export maven project as a jar and run from command promt the logs are displayed on the console instead of writing to a file.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="file_all" fileName="C:/log/logsALL.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="file_all"/>
</Root>
<Logger name="com.api.main" level="INFO">
<AppenderRef ref="file_all"/>
</Logger>
</Loggers>
</Configuration>
When log4j starts up, it shows internal logging on the console (because status=trace in the configuration).
This internal log should show the location of the config file that is being used. Double-check that this is the correct location: I suspect that an old config file is being loaded that logs to the console...

Resources