I have two applications in the same maven project and have given each of them their own configuration file by setting the spring.config.name property for each before invoking SpringApplication.run().
Thus, in the first application, I set spring.config.name to server1 so it looks for server1 instead of application.yml. In the second I have set spring.config.name to server2.
However I would like them to share the same logging configuration. Unfortunately, logging configuration cannot be imported via #PropertySource since logging is already configured before property-sources are read - see Logging section of Spring Boot manual.
Is there any way I can do this?
Spring Boot uses as default Logback. You can put a logback.xml file in src/main/resources to configure the log. And both applications will automatically use this file to configure their logging engine.
You can learn how to configure Logback here: http://logback.qos.ch/manual/configuration.html
A simple example. It will set the log level to INFO and log to the Console:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Related
Spring Boot version : 2.3.5
log4jdbc version : 2.0.0
Hibernate version : 5.4.22 (pulled in by the Spring Boot Starter pack)
I am trying to capture SQL query execution related data from our Spring Boot Java application. Following are the changes I have made in our application:
pom.xml -->
<dependency>
<groupId>com.integralblue</groupId>
<artifactId>log4jdbc-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
application.properties -->
log4jdbc.drivers=oracle.jdbc.OracleDriver
log4jdbc.auto.load.popular.drivers=false
log4jdbc.debug.stack.prefix=^com\.myapp\.subpackage.*
log4jdbc.sqltiming.warn.threshold=25
log4jdbc.jdbc.sqltiming.level=DEBUG
log4jdbc.jdbc.sqltiming.logging.file=${application.logging.path}/log4jdbc.log
logback-spring.xml -->
<springProperty name="LOG4JDBC_SQLTIMING_LOGGING_FILE_NAME" source="log4jdbc.jdbc.sqltiming.logging.file"/>
<springProperty name="LOG4JDBC_SQLTIMING_LOG_LEVEL" source="log4jdbc.jdbc.sqltiming.level"/>
<logger name="jdbc.sqlonly" level="${LOG4JDBC_SQLTIMING_LOG_LEVEL}">
<appender class="ch.qos.logback.core.FileAppender">
<file>${LOG4JDBC_SQLTIMING_LOGGING_FILE_NAME}</file>
<encoder>
<pattern>${FILE_LOGGING_PATTERN}</pattern>
</encoder>
</appender>
</logger>
<!--
<logger name="jdbc.sqltiming" level="${LOG4JDBC_SQLTIMING_LOG_LEVEL}">
<appender class="ch.qos.logback.core.FileAppender">
<file>${LOG4JDBC_SQLTIMING_LOGGING_FILE_NAME}</file>
<encoder>
<pattern>${FILE_LOGGING_PATTERN}</pattern>
</encoder>
</appender>
</logger>
-->
The reason I'd commented out the last logger definition is because it did not work. So I tried to capture the 'sqlonly' logger output. When I run my application, it does generate an empty log file named "log4jdbc.log". However, the other log file capturing the generic application logs is getting generated just the same. Is there some minor mistake that is preventing this configuration setup from working as I expect it to?
I have a Java standalone app. with a main method, with these 2 imports:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
and
private static final Logger log = LoggerFactory.getLogger(PecadorDeLaPradera.class);
In the same folder I also have a logback.xml but I don't know how to tell the program that uses the logback.xml to config the log
I couldn't find any class similar to org.apache.log4j.PropertyConfigurator
I have this logback.xml in the same folder of the Java class I am running:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- trace, debug, info, warn, error, fatal -->
<timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<!-- To enable JMX Management -->
<jmxConfigurator/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>pecador.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>handler.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>1MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<!-- <logger name="org.springframework.orm.jpa" level="debug" /> -->
<logger name="com.calzada.pecador" level="debug" />
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
I also run the appl. with Program arguments:
-Dlogback.configurationFile=/Users/calzada/Dev/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml
and I got this error:
java.util.MissingResourceException: Can't find bundle for base name -Dlogback.configurationFile=/Users/nullpointer/Development/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml, locale en_ES
The library used is logback-classic-1.2.3.jar
Here's how logback configures itself:
Logback tries to find a file called logback-test.xml in the classpath.
If no such file is found, logback tries to find a file called logback.groovy in the classpath.
If no such file is found, it checks for the file logback.xml in the classpath.
If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.
If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
You can also point Logback at a specific configuration file using the system parameter logback.configurationFile. From the docs:
You may specify the location of the default configuration file with a system property named "logback.configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
All the above taken from the docs.
According to your question you have a logback.xml but this file is in the "same folder" as your class. Unless your class is in the root package this means that logback.xml is not in the root of the classpath so Logback will not discover it. In order for Logback to configure itself from this file you can do one of the following:
Place your logback.xml in the root of your classpath (for a Maven project this is as simple as copying logback.xml to src/main/resources)
Run your Java program with -Dlogback.configurationFile=/path/to/logback.xml
In my weblogic server I have two independent application. App1 is old web application with web services using log4j:
# Console logger
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r %-5p %c %x - %m%n
log4j.appender.CONSOLE.Threshold=ERROR
# APP logger
log4j.appender.APP=org.apache.log4j.DailyRollingFileAppender
log4j.appender.APP.DatePattern=.yyyy-MM-dd-HH
log4j.appender.APP.File=${logfile.app}
log4j.appender.APP.encoding=UTF-8
log4j.appender.APP.layout=org.apache.log4j.PatternLayout
log4j.appender.APP.layout.ConversionPattern=%d{DATE} %-5p [%t] %-15c : %m%n
log4j.rootLogger=ERROR, CONSOLE
log4j.logger.cz.isvs=INFO, APP
log4j.logger.org.springframework=WARN, APP
${logfile.app} is replaced during build to target/log/app1.log
When there is just this application deployed everything is working OK.
Second app2 is spring boot with also web services application using logback:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProfile name="default">
<property name="LOGS_HOME" value="/app/app_logs/app2" />
<appender name="appfile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS_HOME}.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGS_HOME}.log.%d{yyyy-MM-dd-HH}</fileNamePattern>
</rollingPolicy>
</appender>
<root level="error">
<appender-ref ref="appfile" />
</root>
<logger name="cz.isvs" additivity="false">
<level value="debug" />
<appender-ref ref="appfile" />
</logger>
</springProfile>
</configuration>
When I deployed second application to my weblogic server then both application is logging into app2.log. I am really confused why and how can this happened
Recently I was facing a similar issue with spring boot deployed in weblogic 12c. After investigation found that spring boot is using spring boot logger starter which has some dependency jar i.e. sl4fj, logback, jcl-over-sl4j, jul-to-slf4j, log4j-over-slf4j.jar etc.
From slf4j manual found this:
The implementation of JCL over SLF4J, i.e jcl-over-slf4j.jar, will
allow your project to migrate to SLF4J piecemeal, without breaking
compatibility with existing software using JCL. Similarly,
log4j-over-slf4j.jar and jul-to-slf4j modules will allow you to
redirect log4j and respectively java.util.logging calls to SLF4J.
The bridging jars are responsible to redirect log4j logs, java util logs, commons logging logs to logback via slf4j. After excluding the bridging jar dependencies got the expected result.
I am getting this highly verbose logging and I want to get rid of it.
1:39:20.187 [main] INFO c.v.c.c.ConfigManager - End XML Read
11:39:20.187 [main] INFO c.v.c.c.ConfigManager - The content of ConfigCfg_pdMetering.xml is ConfigCfg [xxx=yyy]
11:39:37.335 [Thread-1] DEBUG o.a.h.c.p.RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
11:39:37.335 [Thread-1] DEBUG o.a.h.i.c.DefaultHttpClient - Attempt 1 to execute request
The logging comes from a jar I have imported in the project. I modified the logback.xml and log4j.properties in the jar using "jar uf"
logback.xml now looks like:
<?xml version="1.0"?>
<configuration debug="false">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="com.hccl" level="ERROR"/>
<logger name="org.apache.http.wire" level="ERROR"/>
<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
and log4j.properties:
log4j.rootCategory=INFO, stdout
log4j.logger.org.springframework.ws.client.MessageTracing.sent=ERROR
log4j.logger.org.springframework.ws.client.MessageTracing.received=ERROR
log4j.logger.org.springframework.ws.server.MessageTracing=ERROR
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p [%c{3}] %m%n
log4j.logger.httpclient.wire=ERROR
I imported this modified jar and imported it to my project. This has not affected the logging.
Also I am getting the following in the console:
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
I do not know why I am seeing this warning.
Muting Apache http
The packages you want to mute are c.v.c.c. and o.a.h. Those are abbreviations. Also, you specified <logger name="org.apache.http.wire" level="ERROR"/> which doesn't match the above (take the first letters of each package name).
You might have more luck with
<logger name="org.apache.http" level="ERROR"/>
to shut down all org apache http logging (which is probably o.a.h.*).
Muting c.v.c.c
To mute the first two lines beginning with c.v.c.c.ConfigManager, you need to find out the classes. You can do this either by replacing %logger{36} with just %logger to have the full package and class name printed. Or perhaps it's your own class. In this case you can just putt a corresponding logger entry into your file by yourself.
Warning for slf4j-Binding
If slf4j outputs warnings about "actual bindings", you have probably more than one slf4j binding in your classpath. As I read from your question, you try to use both logback and log4j, which won't work - slf4j decides to use logback only. Remove log4j and the log4j.properties. If they get included by maven dependencies, specify exclude tags for those dependencies.
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.