logback how to set destination folder for log files - logback

Is there a way to set a single destination folder, such that I can specify where all log files should be created rather than having to set it on an appender by appender basis?

You can define a property in the logback configuration file an use it as below
<configuration>
<property name="USER_HOME" value="/home/sebastien" />
<appender name="SPRING_LOGS" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/spring.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/myApp.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
Note that logback can read the variables from System properties or a separate properties file too. Follow the manual for more details.

I've wasted a lot of time configuring Logback to work with Spring Boot and I'd like to share my configuration, hoping to save other people from wasting their time.
My example is similar to Andy Dufresne's above, with one key difference - no <property> tag. This was really important in my case because if you include the <property name="logs_dir" value="." /> you won't be able to override it using system properties, which I wanted to do like this:
java -jar -Dlogs_dir=~/newLogsDir yourApp.jar
Also note the default value is set inside the path variable - ${logs_dir:-.}. Hope this helps:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%-20(%d{yyyy-MM-dd HH:mm:ss} %highlight([%-5level])) %msg%n</pattern>
</encoder>
</appender>
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logs_dir:-.}/system.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover monthly -->
<fileNamePattern>system-%d{yyyy-MM}.log.zip</fileNamePattern>
<maxHistory>12</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%-26(%d [%-5level]) %logger{35} - %msg%n</pattern>
</encoder>
</appender>
</configuration>

I have a spring boot app, and I run the fat .jar as a systemd service.
I struggled a couple of hours on how to set the LOG_PATH relative to the user's home dir.
Here is what worked for me:
in application.properties I have:
logging.path=${HOME}/attach_logs
in logback-spring.xml I have:
<springProperty scope="context" name="LOG_PATH" source="logging.path"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/console.log</file>
References:
Getting the user home path in application.properties in Spring Boot
Accessing the application properties in logback.xml
Spring boot logging path
logback how to set destination folder for log files

Related

Java web app catalina logs not written to log file when running in Eclipse

I have a Java web app that I run in Eclipse for my development environment. I use logback for logging to a custom file. The problem is certain logging statements, specifically those that traditionally go to catalina.out, do not end up in my log file. They do show up in my Eclipse console, but not in my custom logback log file.
When I run the same app in tomcat outside of Eclipse (via startup.bat), those catalina logging statements do get captured in a catalina.out file. But when running in Eclipse no catalina.out is created, so those logs don't persist.
Here's my logback-test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- RollingFileAppender that rolls based on size and time -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/mylog.log</file>
<encoder>
<pattern>%date [%thread] %-5level %logger{10} [%file:%line] - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${catalina.base}/logs/mylog.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- each file at most 20MB; keep 10 files worth of history, max total 20GB -->
<maxFileSize>20MB</maxFileSize>
<maxHistory>10</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>logback-test %date [%thread] %highlight(%-5level) %logger{10} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>logback-test %date [%thread] %highlight(%-5level) %logger{10} - %msg%n</pattern>
</encoder>
</appender>
<!-- set logging levels for specific packages -->
<logger name="org.apache.catalina.startup" level="INFO"/>
<!-- set level of the root logger and associate it with both appenders -->
<root level="INFO">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="STDERR"/>
</root>
</configuration>
This answer resolved my question: https://stackoverflow.com/a/5045247/3368818
Apparently, if, while running an app on tomcat via Eclipse, you want to capture the catalina logs in a file, you need to specify that via the Eclipse tomcat launch configuration settings.
I guess in a way this makes sense in that the catalina logs are perhaps not application specific, so maybe outside the scope of logback appenders. But on the other hand, shouldn't they get written to a log file by default anyway, just as they do if you launch your tomcat server outside of Eclipse?

How to override logback config in Sonatype Nexus 3?

I'm running Sonatype Nexus 3.15.0-01 and am a little stumped about how to override the default logback configs.
I created a file called 'logback-overrides.xml' in the 'nexus-data/etc/logback' folder containing the following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/nexus-data/log/myApp.log</file>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
This is essentially just a simple bit of config that should cause logs to be written to '/nexus-data/log/myApp.log'. I restarted the server after adding this file, to confirm it would pick up the new configs.
However, when I check for that file, it's not present. What am I missing here?
I posted this same question on the Sonatype forums here. To sum up the answer I got there, it isn't possible to override the default logback config this way.
Possible workarounds are:
Create your own logback.xml file and build your own Docker image that extends Sonatype’s official image.
Create a volume mount for /opt/sonatype/nexus/etc/logback and customize the logback.xml on your host machine.

logback is not logging application log statements to neither console nor file

we are using logback for our logging and we have following jars in our class path
jcl-over-slf4j-1.7.7.jar
logback-classic-1.1.3.jar
logback-core-1.1.3.jar
slf4j-api-1.7.7.jar
janino-2.7.8.jar
In each app, i have minimal config in logback.xml. Like this
<configuration scan="true" scanPeriod="10 seconds">
<statusListener
class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<contextName>myapp- ${HOSTNAME}</contextName>
<include file="${logback.path}/logback.xml"/>
</configuration>
Then in my file system, I have config like this
<included>
<property name="LOG_HOME" value="C:\\tmp" />
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd-HH:mm:ss.SSS} cn=%contextName [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/application.%d{yyyy-MM-dd-HH-mm}_%i.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>250KB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd-HH:mm:ss.SSS} cn=%contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="INFO"/>
<logger name="com" level="INFO"/>
<root level="INFO">
<appender-ref ref="stdout"/>
<appender-ref ref="file" />
</root>
Now when i deploy my app, i do see logs from spring framework in application.xxx.log file but my actual application code which logs some statement are not showing up in this log file
In my code, i m using slf4j logger factory to get the logger and then just log some dummy statements, like this
private static final Logger logger = LoggerFactory.getLogger(GameService.class);
logger.info("Playing cricket game.......");
I think i figured out the issue. Some where in our lib, we were using common logger util class which was modifying logger context object. As soon as i removed that dependency , things worked out fine. Since that was the old way of logging , we no longer needed common logger util.

Logback empty log file is getting created

In my application is using RolingFileAppender with size based triggering policy main classes initializes log file using logback configuration file. When main class is invoked using a shell script, I see log messages are correctly going to log file that conf define but in addition it also created empty logfile with scriptname
say ABC.sh calls MainClass which is supposed to write to logback123.log now all messages are going to logback123.log fine but there is extra empty ABC.log file is getting created.
Any thoughts on this
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILEOUT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/opt/agents/upgradeSC91Temp/mbs/logs/watchdog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>/opt/agents/upgradeSC91Temp/mbs/logs/watchdog_%i.log</fileNamePattern>
<minIndex>1</minIndex> <maxIndex>10</maxIndex> </rollingPolicy>
<triggeringPolicy class="com.aptare.dc.util.LogbackSizeBasedTriggeringPolicy">
<maxFileSize>20MB</maxFileSize> </triggeringPolicy>
<encoder> <pattern>%d{dd MMM yyyy HH:mm:ss:SSS} %-5p %C{0}.%-5M - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd MMM yyyy HH:mm:ss:SSS} %-5p %C{0}.%-5M - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.aptare.dc.util.LogWriterInitializer">
<level value="INFO"/>
<appender-ref ref="FILEOUT"/>
</logger>
</configuration>

logback dbappender performance

I am looking to use Logback Classic DB Appender (ch.qos.logback.classic.db.DBAppender) in the application. Database is Sybase ASE. I use c3p0 connection pool.
Besides the DB appender, I also have a rolling file appender. Problem is, with the DB Appender I see a drastic decrease in performance. For instance, with only rolling file appender, it takes 13 ms (milli seconds) to log the messages to file. However, with rolling file appender and db appender together, it takes 4510 ms (4.5 seconds) to output the same set of messages to file and database.
Logback DBAppender document mentions that with c3p0 connection pool, it takes around 1ms to insert a single logging statement. Numbers I am seeing are thousand times more than that. Wonder what could be wrong. Following is my logback.xml file:
<configuration debug="true" scan="true">
<property resource="log.properties" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.file.dir}/${project.artifactId}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${project.artifactId}.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>${log.file.rolling.history.days}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
<driverClass>${log.db.driver}</driverClass>
<jdbcUrl>jdbc:sybase:Tds:${log.db.server}:${log.db.port}/${log.db.name}</jdbcUrl>
<serverName>${log.db.server}</serverName>
<databaseName>${log.db.name}</databaseName>
<user>${log.db.user}</user>
<password>${log.db.password}</password>
</dataSource>
</connectionSource>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
Any pointers appreciated.
Thanks.
I found the following from logback manual:
If your JDBC driver supports the getGeneratedKeys method introduced in JDBC 3.0 specification, assuming you have created the appropriate database tables as mentioned above, then no more steps are required, except for the usual logback configuration.
Otherwise, there must be an SQLDialect appropriate for your database system. Currently, we have dialects for PostgreSQL, MySQL, Oracle and MS SQL Server.
for oracle, it said the supported version is :(10.2.0.1)
link:
http://logback.qos.ch/manual/appenders.html#DBAppender
If you have no a Dialect class in your JDBC driver, you can get from hibernate, and put it in your source, I don't know whether it can work.