There are a lot of examples how to redirect program output to file.
I need to set maximum size of the file and roll the file (vanish old messages) or backup old file and start to log to a new one.
Is there any way to do this using OS core stuff?
My customers puzzled me to create log file per application and log there all std out.
The application is long running application and contains with several modules that start in distinct JVMs (these are several applications in other words) and use same log4j.properties file.
I'm using java and log4j, but log4j loggs per package not per application. And it is not suitable for me because two modules could have same package (logger category) but shold be directed to different files.
Application modules are started using .sh scripts.
Thanks
log4j doesn't log on a package basis. You can provide whatever name you like to it.
In your log4j.xml, add this:
<appender name="USERACTION" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/var/logs/useraction.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d%m%n" />
</layout>
</appender>
<logger name="UserAction" additivity="false">
<level value="INFO" />
<appender-ref ref="USERACTION" />
</logger>
Now instantiate your logger like so:
private final static Logger LOG = Logger.getLogger("UserAction");
Everything logged to this logger will then go into the file useraction.log. Do you see the correlation?
The thing with naming the logger after the current class is just convenient, but in no way mandatory.
I use this a lot to differentiate log entries. Some of my classes have multiple logger instances, like one for user action, one for low-level stuff, one for service calls and so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With