Reputation: 1067
I am usually using the folowing bundle of Logback.xml and application.properties to get all I need. Logback.xml:
<configuration>
<!-- External properties -->
<property file="./application.properties" /> <!-- for build -->
<timestamp key="byDate" datePattern="yyyyMMdd"/>
<!-- Send messages to System.out - CONSOLE -->
<appender name="STDOUT" 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{5} - %msg%n</pattern>
</encoder>
<withJansi>true</withJansi>
</appender>
<!-- Send messages to a rolling file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.path}/${spring.application.name}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover. Make sure the path matches the one in the file element or else the rollover logs are placed in the working directory. -->
<fileNamePattern>${logging.path}/${spring.application.name}_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<root level="${logging.level}">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
application.properties:
spring.application.name=MyApp
#Logging Settings
logging.level=INFO
logging.path=/data/MyApp/logs/
logging.config=/data/MyApp/Logback.xml
But now I am trying to write an external Jar which I will include in my "others projects". But my "others projects" already have Logback.xml and application.properties and I would like that external Jar will use it too. I'd try to put nothing of Logback.xml and application.properties in external Jar project and I was thinking that it will find Logback.xml. But it doesn't work.
I don't see any log messages when I call something from external Jar. What should I do in this case? By the way, I am using Maven for building and dependencies. So I'd built external Jar and include it in my projects with:
<dependency>
<groupId>com.myapp.someprovider</groupId>
<artifactId>SomeAppProvider</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>/Users/me/Projects/Java/Server/SomeAppProvider/target/SomeAppProvider-1.0.0.jar</systemPath>
</dependency>
Upvotes: 1
Views: 4341
Reputation: 3300
Logging configuration and properties files should be in the main resources folder (e.g. src/main/resources if you're using maven) of the final binary that you build (e.g. the .war project if you are building a web application). This central log configuration file needs to contain the complete log configuration for the full application, including any jars you depend on.
If you need separate logging configuration files in your modules (e.g. for testing purposes), these should be placed in the test resources folder (e.g. src/test/resources for maven).
As a side note you should typically try to avoid using systemPath in your maven dependencies. Hardcoding a system path like you're doing in your example is begging for trouble, particularly in a project where there are more people involved.
Upvotes: 1