
Hi! I have been playing with logback for a couple of days and find a lot of its functionality amazing! I have a couple of questions that I was wondering if I could address! I'm using logback to log information about a java server that I'm setting up. I want my log files to change every half hour and to never exceed 500MB. I would also like the logging system to cache 16MB of data before flushing to disk! Finally, I want the fileNamePattern to depend on the hostname where I'm running the server! So this are my questions: a. I have found a way for the log files to change every hour, but not every half hour... Is there a way to do this? Is there a way I can define my own RollingPolicy? b. Is there a way for me to define the fileNamePattern somewhere else other than in the logback.xml file? c. Is there a flush() function I can call when my server exists? For b. I have tried something like: LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger logger = lc.getLogger("ROOT"); RollingFileAppender<ILoggingEvent> app = (RollingFileAppender<ILoggingEvent>)logger.getAppender("RootFileAppender"); RollingPolicyBase rollingPolicy = (RollingPolicyBase)app.getRollingPolicy(); rollingPolicy.stop(); rollingPolicy.setFileNamePattern("bidderserver-%d{yyyy-MM-dd-HH:mm:ss}-"+hostname+"-ip.%i.log"); rollingPolicy.start(); But that doesn't seem to work :( I'm attaching my current logback.xml file. Any and all help would be much appreciated! Also, I tried visiting #qos.ch in freenode, but there's noone there except me! Thanks again, Daniel

Now I'm trying this, but I'm getting a NullPointerException on the first start() LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); lc.reset(); //Setup Time Based File Naming and Triggering Policy SizeAndTimeBasedFNATP<ILoggingEvent> timeBasedTriggering = new SizeAndTimeBasedFNATP<ILoggingEvent>(); timeBasedTriggering.setContext(lc); timeBasedTriggering.setMaxFileSize("500MB"); //Setup Time Based Rolling Policy TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>(); rollingPolicy.setContext(lc); rollingPolicy.setFileNamePattern("bidderserver-%d{yyyy-MM-dd-HH:mm:ss}-"+hostname+"-ip.%i.log"); rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedTriggering); //Set the rolling policy as a parent to the triggering policy timeBasedTriggering.setTimeBasedRollingPolicy(rollingPolicy); //Set up a Pattern PatternLayout layout = new PatternLayout(); layout.setPattern("%d{yyyy-MM-dd HH:mm:ss},%t %m%n"); //Set up the Appender RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>(); rollingFileAppender.setContext(lc); rollingFileAppender.setAppend(true); rollingFileAppender.setBufferedIO(true); rollingFileAppender.setBufferSize(16000000); rollingFileAppender.setPrudent(false); rollingFileAppender.setImmediateFlush(false); rollingFileAppender.setLayout(layout); //Start! timeBasedTriggering.start(); rollingPolicy.start(); layout.start(); rollingFileAppender.start(); On Wed, Sep 9, 2009 at 11:17 AM, Nitro Star <nitrostar1@gmail.com> wrote:
Hi!
I have been playing with logback for a couple of days and find a lot of its functionality amazing! I have a couple of questions that I was wondering if I could address!
I'm using logback to log information about a java server that I'm setting up. I want my log files to change every half hour and to never exceed 500MB. I would also like the logging system to cache 16MB of data before flushing to disk! Finally, I want the fileNamePattern to depend on the hostname where I'm running the server!
So this are my questions: a. I have found a way for the log files to change every hour, but not every half hour... Is there a way to do this? Is there a way I can define my own RollingPolicy? b. Is there a way for me to define the fileNamePattern somewhere else other than in the logback.xml file? c. Is there a flush() function I can call when my server exists?
For b. I have tried something like:
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger logger = lc.getLogger("ROOT");
RollingFileAppender<ILoggingEvent> app = (RollingFileAppender<ILoggingEvent>)logger.getAppender("RootFileAppender"); RollingPolicyBase rollingPolicy = (RollingPolicyBase)app.getRollingPolicy(); rollingPolicy.stop();
rollingPolicy.setFileNamePattern("bidderserver-%d{yyyy-MM-dd-HH:mm:ss}-"+hostname+"-ip.%i.log"); rollingPolicy.start();
But that doesn't seem to work :(
I'm attaching my current logback.xml file.
Any and all help would be much appreciated!
Also, I tried visiting #qos.ch in freenode, but there's noone there except me!
Thanks again, Daniel

Hello Daniel, Nitro Star wrote:
Hi!
I'm using logback to log information about a java server that I'm setting up. I want my log files to change every half hour and to never exceed 500MB. I would also like the logging system to cache 16MB of data before flushing to disk! Finally, I want the fileNamePattern to depend on the hostname where I'm running the server!
So this are my questions: a. I have found a way for the log files to change every hour, but not every half hour... Is there a way to do this? Is there a way I can define my own RollingPolicy?
You can define your own RollingPolicy. You would probably need to override the computeNextCheck() method in TimeBasedFileNamingAndTriggeringPolicyBase so that roll over occurs every 30 minutes.
b. Is there a way for me to define the fileNamePattern somewhere else other than in the logback.xml file?
Yes. A convenient way to do this is via variable substitution: http://logback.qos.ch/manual/configuration.html#variableSubstitution
c. Is there a flush() function I can call when my server exists?
By default, FileAppender will flush on every append.
For b. I have tried something like:
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger logger = lc.getLogger("ROOT");
RollingFileAppender<ILoggingEvent> app = (RollingFileAppender<ILoggingEvent>)logger.getAppender("RootFileAppender"); RollingPolicyBase rollingPolicy = (RollingPolicyBase)app.getRollingPolicy(); rollingPolicy.stop();
rollingPolicy.setFileNamePattern("bidderserver-%d{yyyy-MM-dd-HH:mm:ss}-"+hostname+"-ip.%i.log"); rollingPolicy.start();
Look at the various test cases. For example: http://logback.qos.ch/xref-test/ch/qos/logback/core/rolling/RollingFileAppen...
But that doesn't seem to work :(
I'm attaching my current logback.xml file.
The FileNamePattern "dummy-%d{yyyy-MM-dd_HH_mm_ss}.%i.log" tells logback to roll over every second. That's probably not what you want. Try "dummy-%d{yyyy-MM-dd_HH}.%i.log" instead.
Any and all help would be much appreciated!
Also, I tried visiting #qos.ch <http://qos.ch> in freenode, but there's noone there except me!
Look again. HTH, -- Ceki Gülcü Logback: The reliable, generic, fast and flexible logging framework for Java. http://logback.qos.ch
participants (2)
-
Ceki Gulcu
-
Nitro Star