I'm working with log4j 2, version 2.4 on Jboss 6.4, my log4j2.xml is in WEB-INF/classes placed. I can deploy my war without errors about my log4j config, and I can log properly in my RollingFile name="myLog", but something I can't understand is why appear the info's logs for "myLog" in my console appender
private static final Logger loggerBatch = LogManager.getLogger("my.test");
loggerBatch.info("test log");
Config:
<?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] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="myLog" fileName="C:\\Workarea\\my.log"
filePattern="C:\\Workarea\\myLog-%d{dd-MM-yyyy}-%i.log"
append="true">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5MB"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="my.test">
<AppenderRef ref="myLog" level ="info"/>
</Logger>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
<Root level="error">
<AppenderRef ref="Console" />
</Root>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
In addition to Andrey's answer you have declared 3 root loggers. There can only be one. Which one of the definitions will win is undefined.
Check your additivity settings. This will fix the issue
<Logger name="my.test" additivity="false">
Related
I added Log4j2 into my project
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
and added next properties:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx"/>
<!-- Крутое, но бесцветное логирование-->
<!-- <PatternLayout pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}.%M():%L - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}"/>-->
</Console>
<RollingFile name="File" fileName="C:/Users/Mi/output.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz" immediateFlush="true">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
<Logger name="org.apache.logging.log4j.filelogger" level="info" additivity="false">
<AppenderRef ref="File"/>
</Logger>
</Loggers>
</Configuration>
App write logs to console, but not in file. As I wait, app shall write logs into file by url C:/Users/Mi/output.log, it creates automatically, I can't delete it while app is wotking, but there no logs. Please advice, what to do.
You are only logging stuff from org.apache.logging.log4j.filelogger package into the "File" appender. The Logger name property is the package/class reference
You should do something like this to get logging to file instead of console:
<Root level="info">
<AppenderRef ref="File"/>
</Root>
If you want console and file do:
<Root level="info">
<AppenderRef ref="File"/>
<AppenderRef ref="Console"/>
</Root>
I'm attempting to programmatically override the levels of my log4j2 loggers on the fly, but it doesn't seem to be working. Can anyone point out what I might be doing wrong? This is the code that I am currently using to attempt to reset the levels.
final static Level REQUEST = Level.forName("REQUEST", 450);
errorLog.info("Showing Requests in logs");
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig("console");
loggerConfig.setLevel(REQUEST);
LoggerConfig loggerConfigFile = config.getLoggerConfig("file-log");
loggerConfigFile.setLevel(REQUEST);
ctx.updateLoggers();
My configuration file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<RollingFile name="file-log" fileName="${log-path}/DataAdapter.log"
filePattern="${log-path}/DataAdapter-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{MM-dd-yyyy HH:mm:ss} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{MM-dd-yyyy HH:mm:ss} %c{1} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="joy.com.DataAdapter" level="trace" additivity="false">
<appender-ref ref="file-log" level="info"/>
</Logger>
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
</Loggers>
</Configuration>
I finally figured it out. Having the level="" in each appender-ref
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
was overriding my programmatic settings. Removing them allowed me to set the level from the code.
<Root level="info" additivity="false">
<appender-ref ref="console"/>
<appender-ref ref="file-log"/>
</Root>
So I am trying to log the info and error messages to both the console and the log file.
The messages get saved successfully in the log file but do not get logged in the console. Have attached the xml file below:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="basePath">./DemoLogs</Property>
</Properties>
<Appenders>
<RollingFile name="File" fileName="${basePath}/app.log" filePattern="${basePath}/app-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %c{2} -%msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="100 KB"/>
</Policies>
</RollingFile>
<Console name="Console">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="File"/>
</Root>
<Logger name="Demo" level="debug" additivity="false">
<AppenderRef ref="File"/>
</Logger>
</Loggers>
</Configuration>
You need to add a reference to the console appender.
<Root level="all">
<AppenderRef ref="File"/>
<AppenderRef ref="Console"/>
</Root>
I'd like to have log messages shown in three ways:
- on console
- on a single file log/logs/log.log file
- on a monthly changed file of the kind log/logs/log-201610.log
I wrote this configuration (using other questions here and several sources I can't find):
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss,SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="basic" fileName="log/log.log">
<PatternLayout
pattern="%d{dd/MM/yy HH:mm:ss,SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<RollingFile name="RollingFile" fileName="log/logs/app.log"
filePattern="log/logs/log-%d{yyyyMM}.log">
<PatternLayout>
<Pattern>"%d{HH:mm:ss,SSS} [%t] %-5level %logger{36} - %m%n"</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="console" />
</Root>
<Logger name="sistema" level="ALL" additivity="true">
<AppenderRef ref="basic" />
</Logger>
<Logger name="sistemabis" level="ALL" additivity="true">
<AppenderRef ref="RollingFile" />
</Logger>
</Loggers>
</Configuration>
It seems to work properly (doesn't give errors), but I get an empty file in log/logs/app.log . In my file dependencies.xml I have this:
<dependency org="org.apache.logging.log4j" name="log4j-api" rev="2.5" transitive="false"/>
<dependency org="org.apache.logging.log4j" name="log4j-core" rev="2.5" transitive="false"/>
but I am afraid I'm still missing something.
Most likely, one day I'll remove the logging on the single full file (so I need to be able to distinguish among appenders). No size bound is needed.
Where am I wrong?
EDIT: according to what I read in comments, I edited to show the situa
According to comments, I modified dependencies and replaced some lines in the configuration file:
<Pattern>%d{dd/MM/yy HH:mm:ss,SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
<Logger name="sistema" level="ALL" additivity="true"> <!-- name è il nome del pacchetto radice da cui prendere. -->
<AppenderRef ref="basic" />
<AppenderRef ref="RollingFile" />
</Logger>
It seems to work properly. On 1 November, I'll know more about this.
I am using log4j2. But the problem that I am facing is that it logs all logs. I want to ... log from specific package to a specific file & other package to another file. I am using log4j2.xml for configuration.
Please can someone help?
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Loggers>
<Root level="DEBUG" additivity="false">
<AppenderRef level="DEBUG" ref="CONSOLE" />
<AppenderRef level="DEBUG" ref="fileAppender" />
</Root>
<Logger name="com.pkg.test.logging.method" level="DEBUG"
additivity="false">
<Appender-ref ref="fileAppender" level="DEBUG" />
</Logger>
</Loggers>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n%throwable}" />
</Console>
<RollingFile name="fileAppender" fileName="./log.log"
filePattern="./log-%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n}" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</RollingFile>
</Appenders>
Just answered the question.
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Loggers>
<Root level="DEBUG" additivity="false">
<AppenderRef level="DEBUG" ref="CONSOLE" />
</Root>
<Logger name="com.pkg.test.logging.method" level="DEBUG"
additivity="false">
<Appender-ref ref="fileAppender" level="DEBUG" />
</Logger>
</Loggers>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n%throwable}" />
</Console>
<RollingFile name="fileAppender" fileName="./log.log"
filePattern="./log-%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n}" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</RollingFile>
</Appenders>
Removed the <AppenderRef level="DEBUG" ref="fileAppender" /> from root logger. Thus it started logging logs based on packages.
From Log4J Manual:
Adding a specific logger for a class: (you can refer to packages here too)
<Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
<AppenderRef ref="File"/>
</Logger>
Adding a specific appender:
<Appender type="File" name="File" fileName="${filename}">
<Layout type="PatternLayout">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</Layout>
</Appender>
The <Loggers> section of the config looks correct. But, if all other logs not belonging to the expected package is also getting logged, it is the Root logger which is responsible for these logs, and not the package level logger. It may not be active because of the way you are creating the logger instance in your code. E.g: If one is using slf4j to create the logger instance, then this is how we would need to create the logger instance:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Logger logger = LoggerFactory.getLogger(Test.class);
If one gets the logger instance using - Logger logger = LoggerFactory.getLogger("Test") then, the config will not work as expected.
Also, another answer mentioned that the solution was to remove the fileAppender that the package logger was using from the root logger. This, in fact, is not a problem. You are free to use the same appenders in root and package level logger. My answer in context of log4j-api 2.14.1