Log4j2 not working on Tomcat istance - java

I've made a war of my spring boot project that have the following structure.
base
+---logs
+---AuthorizationService
|
+----src -> main ---> java ---> com.project -> (classes)
|
+-> resources
|
+---->application.properties & log4j2.xml
+---ResourceService
|
+----src -> main ---> java ---> com.project -> (classes)
|
+-> resources
|
+---->application.properties & log4j2.xml
After the war build (as you can see the projects have due main modules)
I've uploaded it on a tomcat.
I've found the logfiles in the relative path specified , but when i do something on the app that normally trigger a log message, these files don't respond.
Here's the configuration via xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60" status="WARN">
<Properties>
<Property name="logsRoot">${sys:catalina.home}/logs</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<File name="LogFileAll" fileName="${logsRoot}/AuthorizationLogsAll.log" immediateFlush="true">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</File>
<File name="LogFileError" fileName="${logsRoot}/AuthorizationLogsError.log" immediateFlush="true">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</File>
<!--
<RollingFile name="LOGFILEALL" fileName="${logsRoot}/application.log" filePattern="${logsRoot}/application.log">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
<Policies>
<sizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy min="1" max="4" />
</RollingFile>
-->
</Appenders>
<Loggers>
<Root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="LogFileAll"/>
</Root>
<logger name="com.pcsystem" level="info" additivity="true">
<appender-ref ref="LogFileError" level="warn"/>
<!-- ogni log message marcato con PCSYSTEM_CONSOLE sara' inserito nella console
<appender-ref ref="STDOUT">
<MarkerFilter marker="PCSYSTEM_CONSOLE" onMatch="NEUTRAL" onMismatch="DENY" />
</appender-ref>
-->
</logger>
<!-- opzioni per i loggers del contesto apache tomcat
<logger name="org.apache" level="info" additivity="true">
<appender-ref ref="LogFileAll" />
</logger>
-->
</Loggers>
I have 2 files of configuration 1 for module that target the same path.
Why my app doesn't speak?
Normal logs that in local environment never appear on the log file on "catalinahome"/logs
I've found the log file populated only with start message
EDIT
hey f1sh, you're showing me the light at the end of the tunnel, because the module structure seems to be different.
Actually launching pwd command on tomcat bash i've recovered this path:
home/intranet/tomcat/webapps/intrane-0.0.1/web-inf/classes
inside classe we have the xml but also application.properties. other files and the COM package that lead to
com.project.[...] classes
Is a configuration path problem?

Related

Log4j does not print to console nor write to file

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.

Why Log4j2 doesn't write logs to a file?

This is my configuration log4j2.xml with path to file - src/com/tarasiuk/task_01/log/dataLogger.log:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="FORMAT_MESSAGE">
%d{YYYY-MM-dd HH:mm:ss} [%t] Level:%-7p Class:%c{1}:%-5L - %msg%n
</Property>
<Property name="LOG_FILE">
src/com/tarasiuk/task_01/log/dataLogger.log
</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${FORMAT_MESSAGE}" />
</Console>
<File name="File" fileName="${LOG_FILE}" append="false">
<PatternLayout pattern="${FORMAT_MESSAGE}" />
</File>
</Appenders>
<Loggers>
<Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
<AppenderRef ref="File" />
</Logger>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Structure of my project:
What I do:
change path to log file from src/com/tarasiuk/task_01/log/dataLogger.log to com/tarasiuk/task_01/log/dataLogger.log - no result.
change level in <Logger> from debug to info - no result.
Logs are output to the console - that ok. But why Log4j2 doesn't write logs to a file?
Try with below appender.
May be in your case it is not able to get path from property, so i had provided only name.
So automatically it will create file on same path as your application is.
<Appenders>
<File name="dataLogger" fileName="dataLogger.log" append="false">
<PatternLayout pattern="%level - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%level - %m%n"/>
</Console>
</Appenders>
This will help you.

log4j2 logging to console only

I have a maven web project which requires logging.
I decided to use log4j2 for that purpose and added the required entries in the pom.xml file
Then I created the log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<RollingFile name="RollingFile" fileName="${logs.path}/text.log"
filePattern="${logs.path}/%d{YYYYMMdd}-text.%i.log">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} %-5p %c{1}:%L - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="root" level="info" additivity="false">
<appender-ref ref="RollingFile" level="info" />
<appender-ref ref="Console" level="info" />
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="RollingFile" />
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
I'm starting tomcat with -Dlogs.path="C:\mylogs", where C:\mylogs exists and has public read/write access.
The console logger is working fine, I can see the correct output in my console, the issue is that the file isn't being created, so I have no logfile, everything gets logged to catalina.out only, and the tomcat startup logs don't show errors for log4j.
What am I missing? is there some additional configuration I need to do in order to log to file?
I went through the documentation. You must refer to system properties with sys:, and it seems that tomcat properties are seen as system properties, so I replaced ${logs.path} with ${sys:logs.path} and it worked.
To create a log file with the log4j2 config you first need to define your appender. Here's an example:
<RollingFile name="MGMT"
fileName="${logdir}/mgmt.log"
filePattern="${logdir}/mgmt.%d{yyyy-MM-dd}.log">
<PatternLayout pattern="${layout}"/>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<DefaultRolloverStrategy>
<Delete basePath="${logdir}" maxDepth="1">
<IfFileName glob="mgmt.*.log" />
<IfAccumulatedFileCount exceeds="10" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
Afterwards you just need to define a logger that will use it:
<Logger name="the package or name that is displayed in the log line"
level="ALL"
additivity="false">
<AppenderRef ref="MGMT"/>
</Logger>
If you have a log that has com.company.test as package set that package as name for the logger. It's important that they match.
The field additivity will define if you want to pass the catched log to its parent ( true ) or just let this logger handle it ( false )
EDIT:
your config file might need this:
<Configuration status="info">
<Properties>
<Property name="logdir">${sys:catalina.base}/logs</Property>
<Property name="layout">%d [%t] %-5p %c- %m%n</Property>
</Properties>
<Appenders>
If you don't want to use this you have to define the path static like <RollingFile name="MGMT"
fileName="C:\path\file.log"

Configure log4j 2 using xml

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.

Log4j logging in maven project

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

Categories