We are starting a Spring MVC based web application. We will be using tomcat as the web server.
I need to configure log4j in the application and log to the application specific file and not to the tomcat log files.
e.g. tomcat has its own log files like localhost.log etc. I want something like myAppName.log in the tomcats log folder.
The logging configuration will go in lo4j.xml or log4j.properties in the application war file.
Plus I dont want to hard code the output log file in the web application.
But I am not sure how to do this.
Please help me. As well correct me if I am wrong somewhere.
Do like this, initialize the logger with following code,
Logger log = Logger.getLogger(this.getClass());
log the information like follows,
log.debug("My message");
and place the log4j.xml in your class path. content like follows,
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="infoLogsFile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="MyApplication.log"/>
<param name="Threshold" value="DEBUG"/>
<param name="MaxFileSize" value="100MB"/>
<param name="ImmediateFlush" value="TRUE"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<root>
<priority value ="DEBUG" />
<appender-ref ref="infoLogsFile"/>
</root>
</log4j:configuration>
Do not forget to add the required jars like log4jXXX.jars and apache common logging jars. with this you will be able to see all log messages in MyApplication.log file creates in bin folder of your tomcat.
Try this:
Put the log4j jar as part of the web application
Do not put a configuration as part of your web application
Create your log4j.xml wherever you like
When you start tomcat provide this argument
-Dlog4j.configuration=file:///.../log4j.xml
Related
I'm running Wiremock as a Standalone process (v2.5.1). I've created a Java custom transformer by extending: com.github.tomakehurst.wiremock.extension.ResponseTransformer
My custom transformer then uses some other common code that uses Log4J for logging. With code like:
import org.apache.log4j.Logger;
private static Logger logger = Logger.getLogger(CommonCode.class);
...
logger.error("This is some error");
Is there anyway I can configure Wiremock to output this custom logging? I've tried putting a log4j.xml and log4j.properties file in the classpath. Here's an example of a properties file:
log4j.appender.CUSTOMAPPENDER=org.apache.log4j.RollingFileAppender
log4j.appender.CUSTOMAPPENDER.File=c:/WireMock/logs/custom.log
log4j.appender.CUSTOMAPPENDER.layout=org.apache.log4j.PatternLayout
log4j.logger.com.myorg=DEBUG, CUSTOMAPPENDER
The equivalent log4j.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CUSTOMAPPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="C:/WireMock/logs/custom.log"/>
<param name="datePattern" value="'.'yyyy-MM-dd"/>
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss}] [%r] %5p [%t] (%F:%L) - %m%n"/>
</layout>
</appender>
<logger name="com.myorg" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="CUSTOMAPPENDER"/>
</logger>
</log4j:configuration>
I also have the following JARs in my classpath:
log4j-1.2.17.jar
slf4j-api-1.7.2.jar
slf4j-log4j12-1.7.2.jar
I'd like this custom logging to go to a separate log file from the Default Wiremock verbose logging. Any help would be appreciated.
Once I realised it was simply that log4j could not find the log4j.properties or log4j.xml file I was able to resolve the issue. Basically the log4j properties file must be in the classpath for the Wiremock Java process.
I was running the Wiremock Server via a batch script which looked like this:
"C:/Java/jdk1.8.0_102/bin/java" -Dfile.encoding=UTF-8 -cp "C:/WireMock/lib/wiremock-standalone-2.5.1.jar;C:/WireMock/lib/*" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --port="9091" --extensions "com.myorg.CustomTransformer" --root-dir="C:/WireMock" --verbose > "C:/WireMock/logs/wiremock.log"
My log4j.xml file was in my C:/WireMock/lib directory. I assumed that because I had a classpath entry of cp "C:/WireMock/lib/wiremock-standalone-2.5.1.jar;C:/WireMock/lib/*" which included C:/WireMock/lib/* that it would pick up my log4j properties. I also explicitly added the properties file to this classpath. Neither of these worked (hence this question). My Custom transformer class com.myorg.CustomTransformer is present in a jar also in the lib directory (which is picked up).
However after some research I found 2 ways to get the log4j properties file to be picked up.
Specify the log4j properties location as another -D parameter in the batch script. If I added -Dlog4j.configuration=file://c:/WireMock/lib/log4j.xml
Update my custom jar build script to include the log4j.xml file itself in the jar.
I have 4 applications which configured to log in same file opus-event.log and all have following configuration.
I am facing issue is each application logging in original(opus-event.log) as well as backup file like opus-event.log2015-10-16 and event.log2015-10-17 on date 19 oct.
<appender name="event" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="DEBUG"/>
<param name="file" value="${jboss.server.log.dir}/opus-event.log"/>
<param name="DatePattern" value="yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
Please some one help me to know what issue it is ? is it possible to resolve using log4j configuration without changing log file name?
You can't simply log into one file from 4 separate applications - there needs to be someone who manages and synchronizes the writes to the file.
Possible solutions:
You can use multiple files (one for each application)
If all applications are running in one JVM (e.g. application server), you might be able to configure them to use some shared logging service
Send the log entries to a logging service - all 4 programs would send their logs to a logger application that would write them to file - see Syslog for inspiration
I have a Spring web app running on Wildfly 8.* and for some reason it won't print to the console. I see all the console logs and stack traces fine but the System messages just don't appear.
The problem might be with my log4j setup so I'll post that config;
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC
"-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
I've ran apps on JBoss 7.1 before however without this problem so I'm really at a loss on what could be wrong.
Feel free to ask about any of my other config not sure what would be needed.
Edit:
logger.org.jboss.as.config.level=DEBUG
logger.org.jboss.as.config.useParentHandlers=true
logger.jacorb.config.level=ERROR
logger.jacorb.config.useParentHandlers=true
logger.org.apache.tomcat.util.modeler.level=WARN
logger.org.apache.tomcat.util.modeler.useParentHandlers=true
logger.com.arjuna.level=WARN
logger.com.arjuna.useParentHandlers=true
handler.CONSOLE=org.jboss.logmanager.handlers.ConsoleHandler
handler.CONSOLE.level=INFO
handler.CONSOLE.formatter=COLOR-PATTERN
handler.CONSOLE.properties=autoFlush,target,enabled
handler.CONSOLE.autoFlush=true
handler.CONSOLE.target=SYSTEM_OUT
handler.CONSOLE.enabled=true
handler.FILE=org.jboss.logmanager.handlers.PeriodicRotatingFileHandler
handler.FILE.level=ALL
handler.FILE.formatter=PATTERN
handler.FILE.properties=append,autoFlush,enabled,suffix,fileName
handler.FILE.constructorProperties=fileName,append
handler.FILE.append=true
handler.FILE.autoFlush=true
handler.FILE.enabled=true
handler.FILE.suffix=.yyyy-MM-dd
handler.FILE.fileName=C\:\\wildfly-8.2.0.Final\\standalone\\log\\server.log
formatter.PATTERN=org.jboss.logmanager.formatters.PatternFormatter
formatter.PATTERN.properties=pattern
formatter.PATTERN.pattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} %-5p [%c] (%t) %s%E%n
formatter.COLOR-PATTERN=org.jboss.logmanager.formatters.PatternFormatter
formatter.COLOR-PATTERN.properties=pattern
formatter.COLOR-PATTERN.pattern=%K{level}%d{HH\:mm\:ss,SSS} %-5p [%c] (%t) %s%E%n
Here is my logging config in my standalone deployments folder.
I had a log4j.properties file at the root level of the .war (WEB-INF/classes) from years ago that caused the same issue - deletion fixed it
Since you are using a logging framework, there are 2 ways to fix it:
Remove the logging framework and all the configuration files (why would I do that)
Move your log4j.properties file from src/main/resources to WEB-INF folder.
Quoting: JBoss Docs, Section 'Per Deployment Logging'
Per-deployment logging allows you to add a logging configuration file to your deployment and have the logging for that deployment configured according to the configuration file. In an EAR the configuration should be in the META-INF directory. In a WAR or JAR deployment the configuration file can be in either the META-INF or WEB-INF/classes directories.
The following configuration files are allowed:
logging.properties
jboss-logging.properties
log4j.properties
log4j.xml
jboss-log4j.xml
I usually have trouble configuring logs per deployment in wildfly, but you can use the logging system from wildfly, changing logging configuration of your standalone ou domain config files to this would do the trick:
<root-logger>
<level name="TRACE"/>
<handlers>
<handler name="CONSOLE"/>
<handler name="FILE"/>
</handlers>
</root-logger>
We have a bunch of projects that get deployed in the same Jboss app. server. Each project has its own log4j.properties in its WEB-INF directory.
The idea is for each project to have its own logfile into which it writes its logs.
Any ideas on how this can be achieved.
First of all you don't mention the JBoss version you're using. You've to take in mind that JBoss versions prior to AS7 include its own log4j version (libraries and configuration file: conf/jboss-log4j.xml), so by default JBoss will ignore the log4j.properties file you've in each of your projects.
That said, you have two approaches to configure a log file for each application:
Centralized way: configure it in Jboss, through the
conf/jboss-log4j.xml. This way you won't need to modify your
applications, or
Modify your applications so that they take their own
log4j libraries (you'll have to include them in the ear/war), not
the Jboss ones.
For the first approach you'll have to define an appender for each of your applications, and then categories for the application packages, in the conf/jboss-log4j.xml file. For example, to configure a log file for an application called app1.war which classes are in a package called com.foo.app1, you'd have to add:
<appender name="APP1_APPENDER" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/app1.log"/>
<param name="Append" value="true"/>
<!-- Rollover at midnight each day -->
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] (Thread) Message\n -->
<param name="ConversionPattern" value="%d %-5p [%c] (%t) %m%n"/>
</layout>
</appender>
...
...
<category name="com.foo.app1" additivity="true">
<priority value="INFO"/>
<appender-ref ref="APP1_APPENDER"/>
</category>
You'll have to add a customized set of blocks like this one for each project you want to define a log file for, respectively.
If you prefer the second approach, as said you'll have to include the log4j libraries in each project and isolate each application so that it takes the libraries included in the application not the ones from JBoss. Look at section 10.3.7 of the Jboss logging file for more information about this approach.
I know very little about Java, let alone JASIG CAS.
We are trying to implement CAS on our CentOS 6 server. We are getting the following errors:
java.io.FileNotFoundException: cas.log (Permission denied)
[...snip...]
java.io.FileNotFoundException: perfStats.log (Permission denied)
After some investigation, it seems like tomcat6 is trying to write the log files in its home directory (/usr/share/tomcat6/). I was able to determine this by chown tomcat: /usr/share/tomcat6 and then, after a restart, the log files were created in that directory.
All the other logs though are written to /usr/share/tomcat6/logs which is a symlink to /var/log/tomcat6.
I want to know how do I reconfigure CAS to write these 2 log files to a different directory /usr/share/tomcat6/logs)?
Assuming you have a recent version of CAS, it uses log4j for logging, and you can find the log4j configuration in
$CATALINA_BASE/webapps/cas-server-webapp-VERSION/WEB-INF/classes/log4j.xml
For a standard Tomcat install under CentOS, $CATALINA_BASE would be /usr/share/tomcat.
If your log4j configuration has not been changed, you'll find an appender named "cas" near the top of the file which is responsible for creating cas.log. It looks like this:
<appender name="cas" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="cas.log" />
<param name="MaxFileSize" value="512KB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c] - %m%n"/>
</layout>
</appender>
Further down you'll find another appender named "fileAppender", which creates the perfStats.log file.
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value="perfStats.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
See how the value for the File parameters is just a file name with no directory specified? The log files therefore get created in the $CATALINA_BASE directory. To get them into the Tomcat logs directory where you want them, just change the values to logs/cas.log and logs/perfStats.log.
Note that CAS only reads the log4j config at startup, so once you've made the change you'll have to either undeploy and redeploy CAS or bounce Tomcat for it to take effect.