I have a maven project in NetBeans with hibernate framework. For a long time it was doing its job correctly. After some modification in database, console log is still working but writing to file has stopped without any error or exception. I have deleted log file directory and restart project, but this time it couldn't even create file path directory.
here is my log4j2.xml configuration(in src/main/resources)
I have changed path to D:\logs and D:/logs, and tried different levels (debug, info, error). Also tried to run my "Server.jar" from command line by administrator. And it should be mentioned that I tried the solution in Log4J creates log file but does not write to it and lots of other suggested solutions, but no success achieved.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Properties>
<Property name="path">${sys:user.home}/logs/server</Property>
</Properties>
<Appenders >
<RollingFile name="connections" append="true" fileName="${path}/connections.log"
filePattern="${path}/connections-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="unexpected-events" append="true" fileName="${path}/unexpected-events.log"
filePattern="${path}/unexpected-events-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="readouts" append="true" fileName="${path}/readouts.log"
filePattern="${path}/readouts-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="connections" level="info" additivity="false">
<appender-ref ref="connections" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Logger name="unexpected-events" level="info" additivity="false">
<appender-ref ref="unexpected-events" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Logger name="readouts" level="info" additivity="false">
<appender-ref ref="readouts" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Root level="error" additivity="false">
<!--<AppenderRef ref="console-log"/>-->
</Root>
</Loggers>
</Configuration>
and this is how I used it in my java class:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......
private static final Logger connectionsLog = LogManager.getLogger("connections");
....
connectionsLog.info("device" + deviceNumber + " disconnected");
I have seen weirdness like this when both commons-logging and jcl-over-slf4j are on the classpath together. Excluding commons-logging from the Maven classpath, anywhere it appears, often helps.
By special thanks to user944849 , I understood that the problem could be related to maven dependencies. Then I checked previous version of my project (which has correct logging) and compare dependencies and found out that a library "log4j-to-slf4j" is added (which has added because of importing "org.springframework.boot"). Then first I exclude it from dependency in pom.xml file, as a result it worked fine in NetBeans IDE but not out of it. Then I removed that dependency from pom.xml and problem solved like a charm!
Related
To upgrade my log4j2 from 2.6.2 to 2.15.0, I simply uses maven build to change the pom.xml log4j-core and log4j-api 's version. However, once I have deployed the java application to tomcat. Log4j 2.15.0 doesn't log to the specific file but catalina.out only. When I fallback to the previous 2.6.x without changing anything, things worked as expected. The log4j.xml I have is as shown below and The way that I get the logManager in Java is just
private static Logger log = LogManager.getLogger("Logger");
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<RollingFile name="errorLogging" fileName="/opt/app/tomcat8/logs/test.log"
filePattern="/opt/app/tomcat8/logs/test.log.%d{yyyy-MM-dd}">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
</Root>
<Logger name="Logger" level="info">
<AppenderRef ref="errorLogging" level="info"/>
</Logger>
</Loggers>
</Configuration>
I'm using log4j2 xml configuration with a Routing Appender and a wrapper. I'm passing different appenders to log to different files for different parts of the project. My configuration looks something like this.
<Configuration status="WARN">
<Appenders>
<Routing name="RoutingAppender">
<Routes pattern="${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}"
fileName="logs/${ctx:logFileName}"
filePattern="logs/${ctx:logFileName}.%i.log.gz">
<PatternLayout pattern="d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n"/>
<SizeBasedTriggeringPolicy size="100 MB" />
</RollingFile>
</Route>
<!-- This route is chosen if ThreadContext has no value for key logFileName} -->
<Route key="$${ctx:logFileName}">
<RollingFile name="Rolling-default" fileName="logs/WITHOOUT-THREAD-CONTEXT.log"
filePattern="./logs/${date:yyyy-MM}/default-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<pattern>%d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
</Appenders>
The problem is, I'm getting external library logs(those without appenders) together with my project logs in the second route, although I want only external logs.
I would suggest to use configuration something like below -
<Configuration status="WARN">
<Appenders>
<RollingFile name="RollingFile"
fileName="logs/${ctx:logFileName}"
filePattern="logs/${ctx:logFileName}.%i.log.gz">
<PatternLayout pattern="d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n"/>
<SizeBasedTriggeringPolicy size="100 MB" />
</RollingFile>
<RollingFile name="Rolling-default" fileName="logs/WITHOOUT-THREAD-CONTEXT.log"
filePattern="./logs/${date:yyyy-MM}/default-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<pattern>%d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="application.package" level="info" additivity="false">
<appender-ref ref="RollingFile" />
</Logger>
<Root level="info" additivity="false">
<appender-ref ref="Rolling-default" />
</Root>
</Loggers>
</Configuration>
In this configuration, there are 2 loggers. One logger is for your application having root package name - application.package. This package should contain all sub-packages and classes of your application. It will use RollingFile Appender for logging.
Another is Root logger which can be used for logging external libraries i.e. classes which are not in application.package. It will use Rolling-default Appender.
In above configuration, you are setting fileName through context lookup so you have to set context value in the beginning otherwise you will get error.
I was using log4j2 and it was logging statements for me with no issues. I might have made some changes (moved from info to debug and back) but its quite possibly I might have messed up the config in some other fashion. I am copying my config file below (I have not moved any log4j2 and slf4j jar files from my project). Any thoughts?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="TestApp" packages="">
<Appenders>
<RollingRandomAccessFile name="RollingRandomAccessFile" fileName="logs/test.log" immediateFlush="false" append="false"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger name="FATAL_LOGGER" level="fatal" includeLocation="true" additivity="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<Root level="debug" includeLocation="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
I tested the config in a test project and it works without any issues. Ensure that you do not have any locks on the file and have correct read/write privileges on the file/directory.
This is my XML file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<configuration>
<Appenders>
<RollingFile name="MSIFatalFile" fileName="Fatal.log"
filePattern="Fatal-%i.log">
<Policies>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<PatternLayout pattern="[%d{ISO8601}] %-5level %logger{6} - %msg%n"/>
</RollingFile>
<RollingFile name="MSIErrorFile" fileName="Error.log"
filePattern="Error-%i.log">
<Policies>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<PatternLayout pattern="[%d{ISO8601}] %-5level %logger{6} - %msg%n"/>
</RollingFile>
<RollingFile name="MISFile" fileName="MIS.log"
filePattern="MIS-%i.log">
<Policies>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<PatternLayout pattern="[%d{ISO8601}] %-5level %logger{6} - %msg%n"/>
</RollingFile>
</Appenders>
<loggers>
<logger name="fatalLogger" level="ALL" additivity="false">
<appender-ref ref="MSIFatalFile" />
</logger>
<logger name="errorLogger" level="ALL" additivity="false">
<appender-ref ref="MSIErrorFile" />
</logger>
<logger name="misLogger" level="ALL" additivity="false">
<appender-ref ref="MISFile" />
</logger>
</loggers>
</configuration>
when i put this at my source level it works fine, but when i changes it external directory folder that it is not resolved and file are not updating even not creating when creating logger instances.
I set the clsspath of all jar and xml directory by System.setProprty("java.class.path", path)
but it did't work. pls suggest what i am doing wrong here.
Update tried all possible way but not able to set classpath of(or reference file it more approprate i think) xml file residing at external folder. The problem is that the enviornment that it getting when i place it at project level how would it get when i place it at external folder.
You can use DOMConfigurator.configure("log4j.xml") to load configuration file
Have a look at the Default Initialization Procedure of log4j (1). Use the log4j.configuration system property to specify the initilization file and set the log4j.debug system property to get some more log4j debug output. You will find information about the log4j initialization phase in that output.
E.g.:
java -cp C:\myApp\lib\*; -Dlog4j.configuration=C:\myApp\etc\log4j.xml -Dlog4j.debug MyMainClass
Bind the xml wherever it is.
can use System.setProperty("log4j.configuration", xmlpath).
this works in my case.
I have a problem applying the log4j2.xml auto configuration properly, and I think it has something to do with my folder arrangement.
I'm using maven to add log4j2 libs and arrange my projects as follows:
- one project to contain all "common" classes, used by server and client side of my system.
- another "core" project - the server side application.
Both projects use the same general package hierarchy (like com.foo.specific.package)
In the Common project I define a logger wrapper:
public class LogWrapper
{
static Logger systemParentLogger = LogManager.getLogger("com.foo");
public static Logger getLogger(Class<?> cls)
{
return LogManager.getLogger(cls.getName());
}
}
Moreover, the Common project contains the log4j2.xml file under META-INF (alongside the persistence.xml file for Hibernate usage).
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="PRODUCTION" status="OFF">
<appenders>
<appender type="RollingFile"
name="MyFileAppender"
fileName="logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n"/>
</appender>
</appenders>
<loggers>
<root level="error">
<appender-ref ref="MyFileAppender"/>
</root>
<logger name="com.foo" level="info" additivity="false">
<appender-ref ref="MyFileAppender"/>
</logger>
<logger name="org.hibernate" level="error">
<appender-ref ref=MyFileAppender"/>
</logger>
</loggers>
</configuration>
While running a sample code in the Core project (using the LogWrapper I wrote and some JPA voodoo), I could still see INFO hibernate logs, and no log file was created. I should state that while debugging the code, I could see that the logger fetched was given some weird value "com.foo.core.persistence.PersistenceXMLTest:ERROR in sun.misc.Launcher$AppClassLoader#2f600492"
The log4j2.xml was placed in a "Folder" which in eclipse terms is "not on classpath".
Changing META-INF to be a "source folder" solved the problem.
In addition, the log4j2.xml file was not defined properly.
These are the modifications needed:
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="PRODUCTION" status="OFF">
<appenders>
<RollingFile name="MyFileAppender"
fileName="../Logs/app.log"
filePattern="../Logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="6" modulate="true"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</appenders>
<loggers>
<root level="error">
<appender-ref ref="MyFileAppender"/>
</root>
<logger name="com.foo" level="info" additivity="false">
<appender-ref ref="MyFileAppender"/>
</logger>
</loggers>
</configuration>
Still couldn't make the org.hibernate logger to be redirected to my logs, but at least I got the logger to work