JASIG CAS: How do I change where log files are written to? - java

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.

Related

Adding $USER to Log4J's log file name

I have a standalone Java application myApp.jar running on Linux/bash and its Log4J.xml is configured like the following:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
...
<appender name="DEFAULT_APPENDER" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/apps/my-app/logs/server.log"/>
<param name="MaxFileSize" value="10MB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %c %x %-5p %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="TRACE" />
<param name="LevelMax" value="FATAL" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
It is currently working fine with log files produced as server.log, server.log.1, server.log.2, etc...
However, I have recently found out that, other than me running the application manually with java -jar myApp.jar, the application is also run by cron job (by root). When I try to run myApp.jar manually on terminal using my account, say user123 at the same time when the crob job is running it, my command fails because I lose over file permission when generating the log files. The cron job is generating the log files with owner root and permission 664.
I am wondering if it is possible to change log4j.xml alone so that myApp.jar will produce log files with file names in such fashion: server-{$USER}.log, server-{$USER}.log.1, server-{$USER}.log.2, etc... So for example, the cron job will produce its log files like server-root.log and I, using account user123, will produce my log files in the same directory like server-user123.log
Note: I myself don't have access to root on the server.
Note: I am using Log4J version 1.x

How to set size and amount of log files (stdout, stderr) in tomcat?

I recently deployed a java app on my production server. In which I have installed Tomcat version 8.5 and I have been having problems since the log files stderr and stdout grow in size exaggeratedly and therefore I lower the performance of the server or sometimes it is necessary to restart it since it does not respond. I would like to know how to configure these files so that they create only a certain amount with a specific size. I have tried configuring it through log4j it has given me error when integrating this same a tomcat. I have also configured certain parameters (for example: java.util.logging.FileHandler.limit = 1024) for logging in which the size and quantity are assigned but these do not apply to the configuration and therefore I continue to throw the server .
In a production environment, a good practice is to use a process that works with small log files, and that creates a new one if the critical size has gone.
With log4j you can use a RollingFileAppender. See the minimal configuration :
<appender name="rolling.file.appender" class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="50MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="${catalina.base}/logs/myApplication.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<!-- Root Logger -->
<root>
<priority value="INFO" />
<appender-ref ref="rolling.file.appender" />
</root>
Documentation: https://tomcat.apache.org/tomcat-8.0-doc/logging.html

Log4j issue logging in same file from different application

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

Confuguring log4j for multiple projects in the same app. server

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.

What is the best way to use log4j in the web application?

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

Categories

Resources