import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class AzulMain {
private static final Logger LOGGER = LogManager.getLogger(AzulMain.class.getName());
public static void main(String[] args){
LOGGER.info("Maybe my first Logger works?");
}
}
The imports work fine. I use these jar-files:
log4j-1.2-api-2.17.2.jar
log4j-api.2.17.2.jar
log4j-core-2.17.2.jar
And this is how my XML-file (log4j2.xml) looks. It is in the same folder as my AzulMain:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="log-path">log/${date:yyyy-MM-dd HH-mm-ss-SSS}</Property>
<Property name="archive">${log-path}/archive</Property>
</Properties>
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n
</pattern>
</PatternLayout>
</Console>
<File name="File-Appender-AzulMain" fileName="${log-path}/Azul.log">
<PatternLayout>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n
</pattern>
</PatternLayout>
</File>
<RollingFile name="RollingFile-Appender"
fileName="${log-path}/rollingfile.log"
filePattern="${archive}/rollingfile.log.%d{yyyy-MM-dd#HH-mm}.gz">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="30 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="AzulMain" level="trace" additivity="false">
<AppenderRef ref="File-Appender-AzulMain" level="all"/>
<AppenderRef ref="Console-Appender" level="info"/>
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="Console-Appender"/>
</Root>
</Loggers>
</Configuration>
When I use JavaUtilLogger everything works quite fine so far, I can make it create a file and print to console, however, with log4j nothing works.
I tested deleting the XML file and adding BasicConfigurator.configure() into my main-method, but it still didn't work. If I start my main-method all I get is:
Process finished with exit code 0
What is strange to me is that when I use the command java -Dlog4j.debug -cp AzulMain, it does not show me my configuration as I would expect it, but just what seems to be a very generic help message.
It is my first time, I am using a logger. Does anyone know what the problem might be?
Update:
This helped me as a first step:
BasicConfigurator replacement in log4j2
I deleted the XML-file and used the new
Configurator.initialize(new DefaultConfiguration());
Configurator.setRootLevel(Level.INFO);
And now it works at least in so far as it prints to the console. However, I am still not able to make it use the log4j2.xml file. I tried naming it log4j2-test.xml, too. (Source) It did not make a difference.
Now it works. This is how I did it.
I set up a new project, with just a main-class and added a new xml-file with a file logger only at first, then added a logger to the console, too. I deleted cache in IntelliJ and deleted the Configurator-lines in the code.
This is the new XML-file log4j2.xml that made it work:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="basePath">log/${date:yyyy-MM-dd HH-mm-ss-SSS}</Property>
</Properties>
<!-- File Logger -->
<Appenders>
<!-- Console appender configuration -->
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n
</pattern>>
</PatternLayout>
</Console>
<RollingFile name="fileLogger"
fileName="${basePath}/Azul.log"
filePattern="${basePath}/app-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<!-- Max 10 files will be created everyday -->
<DefaultRolloverStrategy max="10">
<Delete basePath="${basePath}" maxDepth="10">
<!-- Delete all files older than 30 days -->
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<appender-ref ref="fileLogger" />
<appender-ref ref="Console-Appender" level="info"/>
</Root>
</Loggers>
</Configuration>
It seems to me to have been a mistake somewhere in the xml-file that I just couldn't figure out where it is.
Related
I am trying to write my log into different files depending on the logger name...
is it even possible?
how I can use the logger name in the target file name?
this is the XML file I use:
<Configuration status="info">
<Properties>
<Property name="log-path" value="logs/"/>
<Property name="file-name" value="server"/>
<Property name="file-type" value=".log"/>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss.SSS} %-5level %logger - %msg%n"/>
</Console>
<RollingFile name="File" fileName="${log-path}${file-name}${file-type}"
filePattern="${file-name}-%d{yyyy.MM.dd_HH.mm.ss}-%i.log">
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss.SSS} %-5level %logger - %msg%n"/>
<SizeBasedTriggeringPolicy size="1 MB"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="File"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
i have tried to use: <Property name="file-name" value="%logger"/> like in the use in PatternLayout and <Property name="file-name" value="%c{10}"/> witout secssus...
You can't do it like that with the RollingFileAppender. The appender receives log events as configured by your <Loggers> block, if log4j sends it log events with different LoggerNames they will be appended to whatever file is open. A RollingFileAppender writes to one file at a time and rolls when the configured policy tells it to.
You could write to different files by setting up multiple Loggers that target different appenders. Like this:
<Configuration status="info">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss.SSS} %-5level %logger - %msg%n"/>
</Console>
<RollingFile name="FileA"
filePattern="/tmp/A-%d{yyyy.MM.dd_HH.mm.ss}-%i.log">
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss.SSS} %-5level %logger - %msg%n"/>
<SizeBasedTriggeringPolicy size="1 MB"/>
</RollingFile>
<RollingFile name="FileB"
filePattern="/tmp/B-%d{yyyy.MM.dd_HH.mm.ss}-%i.log">
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss.SSS} %-5level %logger - %msg%n"/>
<SizeBasedTriggeringPolicy size="1 MB"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.example.App" level="info" additivity="false">
<AppenderRef ref="FileA"/>
</Logger>
<Logger name="org.example.App2" level="info" additivity="false">
<AppenderRef ref="FileB"/>
</Logger>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
To route each event to a different RollingFileAppender based on some pattern you can use a RoutingAppender.
The RoutingAppender evaluates LogEvents and then routes them to a subordinate Appender. The target Appender may be an appender previously configured and may be referenced by its name or the Appender can be dynamically created as needed. The RoutingAppender should be configured after any Appenders it references to allow it to shut down properly.
You can also configure a RoutingAppender with scripts: you can run a script when the appender starts and when a route is chosen for an log event.
Here is how you could route based on LoggerName:
<Configuration status="info">
<Properties>
<Property name="log-path" value="logs/"/>
<Property name="file-name" value="server"/>
<Property name="file-type" value=".log"/>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss.SSS} %-5level %logger - %msg%n"/>
</Console>
<Routing name="Routing">
<Routes pattern="$${event:Logger}">
<Route>
<RollingFile name="Rolling-${event:Logger}" fileName="${log-path}${file-name}-${event:Logger}${file-type}"
filePattern="${file-name}-%d{yyyy.MM.dd_HH.mm.ss}-%i-${event:Logger}.log">
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss.SSS} %-5level %logger - %msg%n"/>
<SizeBasedTriggeringPolicy size="1 MB"/>
</RollingFile>
</Route>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Routing"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Also, out of interest, if you really want to you can implement your own Appender that does whatever you want with the LogEvent.
Here's a very rough start. It's an Appender that creates a RollingFileAppender per LoggerName. It only works if you use a SizeBasedTriggeringPolicy. You should be able to drop a class like that into your project and use XML like:
<Configuration status="info">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss.SSS} %-5level %logger - %msg%n"/>
</Console>
<FilePerLoggerNameAppender name="File"
filePattern="/tmp/log-%d{yyyy.MM.dd_HH.mm.ss}-%i-$LOGGER$.log">
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss.SSS} %-5level %logger - %msg%n"/>
<SizeBasedTriggeringPolicy size="1 MB"/>
</FilePerLoggerNameAppender>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="File"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
and see log files created with $LOGGER$ replaced with your logger names.
Im usign log4j2 for logging. I need daily logging with keeping a backup of 5 days logs. My log4j2.xml looks as below. My backup files keep on increasing in number eventhough ive limited the number to 5. Where did i go wrong??
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" monitorInterval="300">
<Appenders>
<RollingFile name="roll-by-time-and-size"
fileName="C:\\Users\\ann\\logs\\testing.log"
filePattern="C:\\Users\\ann\\logs\\testing.%d{MM-dd-yyyy}.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy max="5"/>
<!-- <DefaultRolloverStrategy>
<Delete basePath="C:\\Users\\ann\\logs" maxDepth="1">
<IfFileName glob="C:\\Users\\ann\\logs\\test.*.log.gz" />
<IfLastModified age="3" />
</Delete>
</DefaultRolloverStrategy> -->
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n">
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="roll-by-time-and-size"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Instead of the max consider using the delete rule.
I am running my Selenium Automation tests using Maven. From time of execution till end I see so many logs.
I came to know with this code that only .info warnings and .warn goes to console and .debug doesn't.
public static void main(String[] args) {
Logger log = LogManager.getLogger();
log.debug("its a debug message");
log.info("its a info message");
log.warn("its a warning message");
}
Output:
2015-12-24 13:58:21,166 ERROR Logger contains an invalid element or attribute "append"
[INFO ] 2015-12-24 13:58:21.245 [main] DebuggerTest - its a info message
[WARN ] 2015-12-24 13:58:21.247 [main] DebuggerTest - its a warning message
Now I want to pass on a variable in along with my mvn command that will switch on/off any logs in console.
Something like: mvn test --debugging -false So that logs can be seen in generated logs file but not in console.
More info:
I want something like given here:
How to initialize log4j properly?
here user "MATH" advised to use :
Logger.getRootLogger().setLevel(Level.WARN); if don't want to see debug logs
I want to enable/disable this from mvn command line.
More info 2:
this is how my log4j2.xml looks:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<RollingFile name="trace-log" fileName="${log-path}/rnf-info.log"
filePattern="${log-path}/rnf-trace-%d{yyyy-MM-dd}.log" append="false">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile name="debug-log" fileName="${log-path}/rnf-debug.log"
filePattern="${log-path}/rnf-debug-%d{yyyy-MM-dd}.log" append="false">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.rnf" level="debug" additivity="false" append="false">
<appender-ref ref="trace-log" level="info"/>
<appender-ref ref="debug-log" level="debug"/>
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
</Root>
</Loggers>
</Configuration>
I think you could use -Dlog4j.configuration=<path> to set the configuration of the logger to whatever you want directly on command line.
See documentation here: http://logging.apache.org/log4j/1.2/manual.html
What you can try to use are variables
log4j2 configuration
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LEVEL">WARN</Property> <!-- default value -->
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="${sys:LEVEL}">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
and to change the default value from WARN to DEBUG you can use
-DLEVEL=DEBUG
Edit:
In your configuration the level for console is fixed - static, set to INFO, but you want to have a dynamic behavior.
You need to add another property
<Properties>
<Property name="log-path">logs</Property>
<Property name="LEVEL">WARN</Property> <!-- default value -->
</Properties>
In which I set WARN as a default level, so by default, there will be less messages in console.
Then I'm referencing the property in Root logger configuration ${sys:LEVEL}
<Root level="${sys:LEVEL}">
but it can be specified from command line as standard JVM parameter -D....
So if you want more messages in console, you will run mvn test -DLEVEL=DEBUG
I've implemented async logging with log4j 2, but now I need to change log filename every hour, eg 2015-11-19/log-12.00.log, 2015-11-19/log-13.00, etc. (Soulutions I've found didn't work, may be I did something wrong).
I have following log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Don't forget to set system property
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
to make all loggers asynchronous. -->
<Configuration status="WARN">
<Appenders>
<!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
<RandomAccessFile name="RandomAccessFile" fileName="async.log" immediateFlush="false" append="true">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
</RandomAccessFile>
</Appenders>
<Loggers>
<Root level="info" includeLocation="false">
<AppenderRef ref="RandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
How to achive this?
You should have a look at TimeBasedTriggeringPolicy. Basically it causes a rollover once the date/time pattern no longer applies to the active file.
Have't tried this but this should work for you.
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN" >
<appenders>
<Async name="Async">
<AppenderRef ref="logfile" />
</Async>
<RollingRandomAccessFile name="logfile" fileName="async.log" filePattern="log-%d{HH}.00.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="24"/>
</RollingRandomAccessFile>
</appenders>
<loggers>
<root level="INFO" includeLocation="false">
<AppenderRef ref="Async"/>
</root>
</loggers>
</configuration>
The TimeBasedTriggeringPolicy interval attribute controls how often a rollover should occur based on the most specific time unit in the date pattern. In you case it is hours that is %d{HH} from file pattern.
Am little new to web applications, recently I was in need to employ a logging mechanism and for that I choose Log4J2, I went through there guide, and downloaded required libraries. This is what so far I did.
1. Added following jars to web-inf/lib
-- log4j-core2.1.jar
-- log4j-api-2.1.jar
-- log4j-web-2.1.jar
2. Added below xml as, log4j2.xml in java/src directory
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="NONPROD" status="OFF">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<!-- <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n -->
<!-- </pattern> -->
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="info-log" fileName="${log-path}/web-info.log"
filePattern="${log-path}/web-info-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/web-error.log"
filePattern="${log-path}/web-error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
<RollingFile name="debug-log" fileName="${log-path}/web-debug.log"
filePattern="${log-path}/web-debug-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
<RollingFile name="trace-log" fileName="${log-path}/web-trace.log"
filePattern="${log-path}/web-trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.demo.web.log4j2.file" level="debug"
additivity="false">
<appender-ref ref="trace-log" level="trace" />
<appender-ref ref="error-log" level="error" />
<appender-ref ref="debug-log" level="debug" />
<appender-ref ref="info-log" level="info" />
</Logger>
<Logger name="com.demo.web.log4j2.console" level="all"
additivity="false">
<AppenderRef ref="console-log" />
</Logger>
<Root level="info" additivity="true">
<AppenderRef ref="console-log" />
</Root>
</Loggers>
</configuration>
3. third and last i wrote an small web service to test logging
#Path("/register")
public class Register {
private DataManager mManager;
private static final Logger LOG = LogManager.getLogger(Register.class);
public Register(){
mManager = DataManager.getInstance();
}
#GET
#Path("/{param}")
public Response getMsg(#PathParam("param") String msg) {
LOG.error("Not supported operations");
return Response.status(Response.Status.BAD_REQUEST)
.entity("Not supported").build();
}
But in console, upon trigger get request this is all i got printed.
INFO: Server startup in 35959 ms
Not supported operations // this is not in pattern i supplied in log4j2.xml
// this is my pattern
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
I figure out this is probably due to some thing gone wrong, and am struggling to find the actual cause, being totally new to web programming, please help me out to configure my logger.
Thanks,
Techfist
Your servlet container is using some other logging framework which logs to console. Figure out which logging framework that is and use one of log4j2's bridge libraries to redirect output to your log4j2 setup.
Alright, I found the cause behind this, Issue was primarily happening due to two reasons.
First I was suppose to exclude log4j* pattern from jarsToSkip attribute in catilina property
Second, i had kept two log4j2.xml one inside web-inf other inside java/src, it was supposed to be only present at java/src. two files were not required.
Third, but not mandatory, before deployment just check if log4j2.xml is included inside web-inf/classes or not, if not then simply add it.
that's it, after following this issue was resolved now am getting proper logs.