I have a Spring Boot application with the following setting in logback-spring.xml
<logger name="org.hibernate.sql" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
But this doesn't log the generated SQL unless I change sql to uppercase like this
<logger name="org.hibernate.SQL" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
which then produces log statements like this
...org.hibernate.SQL : select count(disabled0_.user)
Also worth noting is that IntelliJ doesn't link to anything when I hover over SQL in the logger name. It links to the package when hovering overorg.hibernate but stops at SQL. But it does link to other more specific classes like this one
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" additivity="false" >
<appender-ref ref="CONSOLE"/>
</logger>
in that one, hovering over BasicBinder does link to the class, and results in statements like
o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR]
Why does SQL have to be uppercase to work?
I am using spring-boot-starter-data-jpa:1.5.10.RELEASE
SQL has to be in uppercase, because the logger name is case-sensitive. See this answer.
IntelliJ can't link to the class, because it's not actually a class, just the name of the Hibernate logger. By convention, logger names are the class name, but that's not a requirement, they can be any string.
Related
Following project structure is present in java application with log4j2 xml configuration .
com.app.package.one
class1.java
class2.java
class3.java
class4.java
com.app.package.two
class5.java
class6.java
class7.java
class8.java
whole application to print the loggers on following all three conditions to be matched
condition1-> print the error mode logger for all the classes
condition2-> print if the logging statement has "exception" in part of logging statement
condition3-> for package 2's class 7 alone it needs exclusion that to display if the logging statement contains exception or sometext
i have added the following log4j2 xml configuration which is satisfying the condition1 and condition2 . but cannot able to achieve the condition 3 in the same configuration .
working configuration for condition 1 and 2
<Logger name="com.app.package" level="error">
<RegexFilter regex=".*(?i)exception(?-i).*" onMatch="ACCEPT" onMismatch="DENY"/>
</Logger>
not working for condition 3
<Logger name="com.app.package" level="error">
<RegexFilter regex=".*(?i)exception(?-i).*" onMatch="ACCEPT" onMismatch="DENY"/>
</Logger>
<Logger name="com.app.package.two.class7" level="error">
<RegexFilter regex=".*(?i)exception(?-i).* | .*(?i)sometext(?-i).*" onMatch="ACCEPT" onMismatch="DENY"/>
</Logger>
is this possible to do a package level filter and as well class level filter both at a time for same class ?
any input on this will be help full,
thanks in advance... :-)
For condition 3
there were two problem on the logging configuration
1)appender information to be added
2)on regex space before and after pipe | symbol
the altered configuration working for all there condition is looks below
<Logger name="com.app.package" level="error">
<RegexFilter regex=".*(?i)exception(?-i).*" onMatch="ACCEPT" onMismatch="DENY"/>
<AppenderRef ref="LogToConsole"/>
</Logger>
<Logger name="com.app.package.two.class7" level="error">
<RegexFilter regex=".*(?i)exception(?-i).*|.*(?i)sometext(?-i).*" onMatch="ACCEPT" onMismatch="DENY"/>
<AppenderRef ref="LogToConsole"/>
</Logger>
I am upgrading from log4j1.x to log4j-2.8.2
I am trying to convert my existing log4j.properties file to equivalent log4j2.xml.
My first question is can I write log4j2.proeprties file instead of log4j2.xml file?
I found some equivalent tags to log4j.proeprties file in log4j2.xml.
I did not find tags for the below lines. can anybody suggest on this?
# LoggerFactory for ESAPI to utilize Log4J
log4j.loggerFactory=org.owasp.esapi.reference.Log4JLoggerFactory
log4j.category.Default=ALL, CONSOLE, RollingFile2, edelivery
log4j.rootCategory=OFF
log4j.logger.org.apache=INFO
log4j.logger.IntrusionDetector=ERROR
log4j.logger.org.springframework=WARN
Thanks in advance.
What i am sure is:
See the last three lines:
log4j.logger.org.apache=INFO
log4j.logger.IntrusionDetector=ERROR
log4j.logger.org.springframework=WARN
I think in log4j.xml they should be instead of like these below:
<Logger name="org.apache" level="INFO"/>
<Logger name="IntrusionDetector" level="ERROR"/>
<Logger name="org.springframework" level="WARN"/>
And what I am not sure is:
these two lines:
log4j.category.Default=ALL, CONSOLE, RollingFile2, edelivery
log4j.rootCategory=OFF
I guess you have configured some appenders with the name in above line like:
<appender name="CONSOLE">
other proerties....
</appender>
so i think you could try to use these in your log4j.xml:
<root>
<appender-ref ref="ALL" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="RollingFile2" />
<appender-ref ref="edelivery" />
</root>
And What I really don't know is:
log4j.loggerFactory=org.owasp.esapi.reference.Log4JLoggerFactory
I Know nothing about esapi so i could not suggest how to deal with it, hope someone else to give more answer.
This question already has answers here:
Spring Boot Unit Test ignores logging.level
(6 answers)
Closed 5 years ago.
My Spring Boot testing stack is Maven + Surefire + JUnit4. I am annotating the tests with #RunWith(SpringJUnit4ClassRunner.class).
I have application.properties in my project root with this line:
logging.level.root=INFO
This controls the logging when running the Spring boot app and it works on normal runs.
However, whenever I run any JUnit4 tests, I am spammed by pages of DEBUG output like this:
....
17:43:20.500 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'autoConfigurationReport'
17:43:20.500 [main] DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader - Registered bean definition for imported class 'org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration'
17:43:20.501 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.boot.autoconfigure.condition.BeanTypeRegistry'
17:43:20.502 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'autoConfigurationReport'
....
All this spam makes it almost impossible to see the actually relevant parts. How can I apply the logging levels to test output?
I haven't set any logging explicitly, and according to the docs Logback is used by default.
From a general perspective, you can provide a seperate logback-test.xml-file at the test-resource level. In this file you can add settings regarding the log-level targeted at the output you'd like such as:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</layout>
</appender>
<logger name="com.your.package" level="DEBUG">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.springframework" level="WARN">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.hibernate" level="WARN">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.eclipse" level="WARN">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="jndi" level="WARN">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.apache.http.wire" level="WARN">
<appender-ref ref="CONSOLE"/>
</logger>
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
Hope this helps you somewhat on the path to decreasing the log output. More is documented at logback's own page:
https://logback.qos.ch/manual/configuration.html
Its mentioned in the top section:
Let us begin by discussing the initialization steps that logback follows to try to configure itself:
1.Logback tries to find a file called logback-test.xml in the classpath.
2.If no such file is found, logback tries to find a file called logback.groovy in the classpath.
3.If no such file is found, it checks for the file logback.xml in the classpath..
4.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.
5.If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
In simple terms what is the result of making additivity="true" or additivity="false" when adding a Log4j configuration for a specific class like this?
<Logger name="com.mypage.glass.TryWindow" level="INFO" additivity="true">
<AppenderRef ref="console"/>
<AppenderRef ref="file"/>
</Logger>
By default, a logger inherits the appenders from its ancestors. By setting additivity="false", you prevent this behaviour.
In your example, there may be appenders associated with com.mypage.glass or com.mypage or even the root logger that would be inherited if you don't set that property to false.
I have a class with several methods. I would like each method to output to a different log file. With a logback.xml file like this, it logs ALL logging calls from the class.
<logger name="com.mincom.ellipse.conversion.BatchConverter" level="INFO">
<appender-ref ref="FILE" />
</logger>
How do I get per method logging calls. I'm sure it's very simple, but I cannot seem to see the answer in the doco.
Haven't used logback, but in log4j and others you can setup loggers with any name you like. Using the classes package and name is just a convention. So I'd setup Multiple loggers in your class, something like this:
Logger logA = LogFactory.getLogger("LogA");
Logger logB = LogFactory.getLogger("LogB");
public void methodA() {
logA.debug(...);
}
public void methodB() {
logB.debug(...);
}
And then in your logback setup ...
<logger name="LogA" level="INFO">
<appender-ref ref="FILE-A" />
</logger>
<logger name="LogB" level="INFO">
<appender-ref ref="FILE-B" />
</logger>
Should work. Probably needs some tweaking :-)