In my current project i want to create log files date wise i.e. log files should reside inside
folder having name as date.
Also archiving should happen at that particular folder.
Current appender that i am using looks like this (it does archiving of log file based on size).
<appender name="AUDITFILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${PROJECT_HOME}\\projectname\\audits\\myproject.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${PROJECT_HOME}\\projectname\\audits\\myproject_%d{yyyy-MM-dd}.%i.zip
</fileNamePattern>
<maxHistory>10</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10KB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%date %msg%n
</pattern>
</encoder>
</appender>
As mentioned in the documentation for fileNamePattern, you can specify multiple %d tokens so as put the date in the folder name of the archive filename:
<fileNamePattern>${PROJECT_HOME}\\projectname\\audits\\%d{yyyy-MM, aux}\\myproject_%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
Note that only one %d token can be primary, all other tokens must be marked as auxiliary by passing the 'aux' parameter.
But if you also want to put it in the file name of the non-archive filename, then you have two options:
use a <timestamp /> element to set a variable which you use in the path. But this timestamp will only be set once at startup, so it's good for batch runs but not for services.
Do like (1) above, but wrap the <appender/> and the <timestamp /> with a SiftingAppender, which will enable the timestamp to be re-evaluated, if using version of logback >=1.0.12. Not sure exactly how you'd want to configure the SiftingAppender. But hopefully that will put you on the right track.
Related
I have a Java server application for which I log all the general data to server.log and each individual client to its own hostname.log file. I want to put the hostname.log files in a different directory that server.log ideally in some organized fashion since there are thousands of hostname.log files.
This is the config I'm using now:
<appender name="SiftAppender" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>descriminatorid</key>
<defaultValue>server</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${descriminatorid}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${cmb.log.dir}/${descriminatorid}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${cmb.log.dir}/archive/${descriminatorid}.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<!-- keep ${maxbackupindex} days' worth of history capped at ${maxfilesize} total size -->
<maxHistory>${cmb.log.maxbackupindex}</maxHistory>
<totalSizeCap>${cmb.log.maxfilesize}</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %msg%n</pattern>
</encoder>
</appender>
</sift>
</appender>
This config logs everything to $cmb.log.dir. What I'd like is something like this:
Server logs: $cmb.log.dir/server.log
Client logs: $cmb.log.dir/client/${descriminatorid}.log
Or even better would be:
Client logs: $cmb.log.dir/client/${firstLetter}/${descriminatorid}.log
where ${firstLetter} is the first letter of $descriminitorid. That way the logs would be distributed in a more scalable hierarchy.
The solution I found was very simple. Since I am generating ${descriminatorid} in my java code I simple included the directory structure i wanted in that value. So instead of "descriminatorid" being "hostname" it's now "client/firstletter/hostname".
I was definitely trying to overthink this one.
I'm using the logback in my application and following rolling file appender.
<appender name="OVERALL_SYNCHRONOUS" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Append>true</Append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Daily rollover -->
<fileNamePattern>${logRoot}/${dirName}/application_overall.log.zip</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{dd.MM.yy HH:mm:ss.SSS} [%-5level] %65.65(%logger{0}.%aspectjFreeMethod\(%line\)): %X{MDC_CtrlName}%msg%n</pattern>
</encoder>
</appender>
This works fine most of the times but sometimes on the day change it zips the previous that is as expected but does not change the directory for the new log file. It creates the new file in the same folder of the previous day.
When day changes from 18.11.15 to 19.11.15 application zipped the application_overall.log as it should and create "application_overall.log.zip".
But application continues to log in the application_overall.log in the folder "2015-11-18".
No application_overall.log was created in the folder "2015-11-19" whereas all the other log files of the application were zipped in folder "2015-11-18" and a new log file is created in the current date folder i.e. "2015-11-19".
After restarting, a new log file of application_overall.log was created in the folder "2015-11-19".
EDIT:
logroot and dirName are defined as below :-
<insertFromJNDI env-entry-name="java:comp/env/log/logRoot" as="logRoot"
scope="context"/>
<insertFromJNDI env-entry-name="java:comp/env/log/dateDirNamePattern" as="datePattern" scope="context"/>
<property name="dirName" value="%d{${datePattern:-yyyy-MM-dd}}" scope="context"/>
What could be the possible reason for this behaviour and how can I solve this issue?
I'm using Logback and I want to log some strings to a log file and let it gzip the file when I'm done.
I was following the example of:
Programmatically configure LogBack appender
But haven't figured out how to, when I finish logging, tell it to gzip the file.
All the examples show to use fileNamePattern. The examples I've seen show to to define this in logback.xml, but I'm trying to do this by code.
Would appreciate some pointers / examples for this :)
<!-- Time and Size based: Roll every day and split big file in smaller peaces -->
<appender name="ROOT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/root.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/root-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date %-5level [%thread] - %mdc{loginName} - [%logger]- %msg%n</pattern>
</encoder>
</appender>
Notice the ".gz": this indicates that the logfile will be compressed. Replace this with .zip to use a zip-file.
There are some limitations, but is basically the easiest flow. The docs state.
Just like FixedWindowRollingPolicy, TimeBasedRollingPolicy supports automatic file compression. This feature is enabled if the value of the fileNamePattern option ends with .gz or .zip.
My java EAR application runs on 2 application servers. Each write the logs locally with log file names. I want to know how to add the unique app server name to the log file name that is being written in each server. This would help me identify the files from each server when I am looking into the log files after downloading them from server and while sending to coworkers for debugging. Currently we have to put them into separate folders as both folders have files with the same names. Thank you.
Following is the snippet from logback.xml that names the file
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logfile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>logfile-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 10MB -->
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
If you use groovy configuration for logback, you can try
import java.net.InetAddress
println InetAddress.getLocalHost()
This will give you the local server name and IP address.
I'm working on a project where I thought I would try using logback-classic for debugging and log rotation. I'm using this in a Maven context for building and creating a .war file to be deployed in JBoss 7.1 Application Server.
I've placed a logback.xml file in the resources folder in the code and a logback-test.xml in test/resources.
The active jar I'm using is the SLF4J to print the actual debugging.
public class MyClass extends MultiActionController {
private Logger logger = LoggerFactory.getLogger(MyClass.class);
public MyClass() {
logger.debug("hello");
}
}
When I run a JUnit test on the code in Maven itself it works, but after building a .war file i don't get any debugging in STDOUT nor can I find a file created.
I know that I've removed the actual logging from STDOUT in the config file, but where is the logging going...
the logback.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/myproject.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyMMdd HH:mm:ss.SSS} [%-5.5level] [%-25.25logger] %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
The folder "logs" do I need to explicit create it in the JBoss instance or need to refer relative to it for this to work? Or is there something I missed? Do I need to put the logback.xml file in the JBoss instance?
best,
Henrik
Firstly you should ensure that the logback.xml is at the classpath, e.g. WEB-INF/classes/logback.xml as the mentioning at Chapter 3: Logback configuration.
The Chapter 4: Appenders mentions about FileAppender and RollingFileAppender properties as
file *String* The name of the file to write to. If the file does not exist, it is created. On the MS Windows platform users frequently forget to escape back slashes. For example, the value c:\temp\test.log is not likely to be interpreted properly as '\t' is an escape sequence interpreted as a single tab character (\u0009). Correct values can be specified as c:/temp/test.log or alternatively as c:\temp\test.log. The File option has no default value.
If the parent directory of the file does not exist, FileAppender will automatically create it, including any necessary but nonexistent parent directories.
The example is for logback.xml is as the following:-
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
I hope this may help.
You need to define the file-tag (as the previous answers already said). But you also need to know how it's interpreted. Logback sees it as relative to the application it's used in. So if you only use
<file>logs/<myfile>.log</file>
logback should create a "logs"-folder in the root of your JBoss (more specific: the folder where the .sh-script you start it with is located). Check from the root of your JBoss to see if you can find your logs-folder and file from there.