how to configure specific package in log4j.properties - spring

I need to have log4j configuration for specific classes. Does anyone know how can we do it with log4j.properties.
As against, we can add logger with class/package in log4j.xml
<logger name="org.springframework.security">
<level value="info" />
<appender-ref ref="console" />
</logger>
I want equivalent configuration in log4j.properties?

The pattern is log4j.logger.<class or package name>=<level>[, appender]
log4j.logger.org.springframework.security=INFO
log4j.logger.org.springframework.security=INFO, console

Related

Show request header informations in Jetty inside SpringBoot

Does anyone have an information on how to enable request header logging (log4j2 configured over xml) inside Jetty in Spring Boot?
This didnt work:
<Logger name="org.eclipse.jetty.server.Server" level="TRACE" additivity="false">
<appender-ref ref="SyslogInfoLogFile" />
</Logger>

Spring Profile not available in Logback when run via Maven

I've set up logback.xml to select which appender to use based on the active Spring profile. The technique works perfectly when I run the app using
java -jar -Dspring.profiles.active=local path/to/target/application.war
but not when I run it using the Spring Boot Maven Plugin, e.g.
mvn spring-boot:run -Drun.profiles=local
Here's the relevant section of the logback.xml
<root level="INFO">
<if condition='"${spring.profiles.active}".contains("local")'>
<then>
<appender-ref ref="CONSOLE"/>
</then>
<else>
<appender-ref ref="FILE"/>
</else>
</if>
</root>
I will note that the profile does show up correctly in the application itself, just isn't available when processing logback.xml.
The problem also manifests when running from the IntelliJ IDE.
Is there another way to use the Maven Spring Boot Plugin to cause the profile to be visible to the logback.xml parser, and would it work for IntelliJ, as well?
Have you tried to configure logback through logback-spring extension?
In your case logback-spring.xml could look like this:
<?xml version="1.0" encoding="UTF-8"?>
<include resource="org/springframework/boot/logging/logback/base.xml"/><!-- include this config if you want to use spring-boot's built-in appenders -->
<configuration>
<root level="INFO">
<springProfile name="local">
<appender-ref ref="CONSOLE"/>
</springProfile>
<springProfile name="otherprofile">
<appender-ref ref="FILE"/>
</springProfile>
</root>
</configuration>
More information about available options in logback-spring extensions:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-logback-extensions

Possible: Override logback logging level with environment variables

I currently have the following logger defined in my logback.xml:
<logger name="Event" level="INFO" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
Is it possible to override the level here using an environment variable? I am using SpringBoot.
I have tried having these defined as ENV variables:
LOGGING_LEVEL_ROOT=ERROR
LOGGING_LEVEL_EVENT=ERROR
LOGGER_LEVEL_EVENT=ERROR
However none of these worked.
Spring Boot supports springProfile extension for the logback configuration file:
<springProfile name="test"> </springProfile>
Documentation
Also there is another way to do it using LoggingApplicationContextInitializer.

Get spring framework logging in wildfly

I was trying to get Spring 4 to log in Wildfly 8.2. I wanted spring to use the wildfly logging configuration.
All the examples I could find were trying to complicate this process by adding additional logging frameworks and configuration.
So here is how I did it, for prosperity.
To get spring 4 to log in wildfly 8, I had to add the following to the logging subsystem config in the standalone.xml file.
<add-logging-api-dependencies value="false"/>
<use-deployment-logging-config value="false"/>
Additionally to get debug logging
<logger category="org.springframework">
<level name="DEBUG"/>
</logger>
Here is the full subsystem config:
<subsystem xmlns="urn:jboss:domain:logging:2.0">
<add-logging-api-dependencies value="false"/>
<use-deployment-logging-config value="false"/>
<console-handler name="CONSOLE">
<level name="DEBUG"/>
<formatter>
<named-formatter name="COLOR-PATTERN"/>
</formatter>
</console-handler>
<periodic-rotating-file-handler name="FILE" autoflush="true">
<formatter>
<named-formatter name="PATTERN"/>
</formatter>
<file relative-to="jboss.server.log.dir" path="server.log"/>
<suffix value=".yyyy-MM-dd"/>
<append value="true"/>
</periodic-rotating-file-handler>
<logger category="com.arjuna">
<level name="WARN"/>
</logger>
<logger category="org.apache.tomcat.util.modeler">
<level name="WARN"/>
</logger>
<logger category="org.springframework">
<level name="DEBUG"/>
</logger>
<logger category="org.jboss.as.config">
<level name="DEBUG"/>
</logger>
<logger category="sun.rmi">
<level name="WARN"/>
</logger>
<logger category="jacorb">
<level name="WARN"/>
</logger>
<logger category="jacorb.config">
<level name="ERROR"/>
</logger>
<root-logger>
<level name="INFO"/>
<handlers>
<handler name="CONSOLE"/>
<handler name="FILE"/>
</handlers>
</root-logger>
<formatter name="PATTERN">
<pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
</formatter>
<formatter name="COLOR-PATTERN">
<pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
</formatter>
</subsystem>
The answer depends on what causes the deployment to break in your deployment.
If you have any of the following logging classes in your classpath that can cause logging to break: logging.properties, jboss-logging.properties, log4j.properties, log4j.xml, jboss-log4j.xml
So for example just run this code (either in your app or in a debugger) and it'll show you if you have the file in the classpath
getClass().getResource("/logging.properties")
Repeat for each of the log files specifed above, if any of those return non-null then you have found your culprit.
At that stage you can either remove the problem logging file, or alternatively use Rian's suggestion of adding <use-deployment-logging-config value="false"/> (you don't need to use add-logging-api-dependencies in that scenario thou).
Another potential issue if you have multiple logging jar files. Keep in mind that wildfly will automatically provide several of those unless you use <add-logging-api-dependencies value="false"/>
I had this similar problem and I got it working thanks to the first answer posted by Rian. Here is my working system:
Server: jBoss EAP 7.1
Framework: Spring 5
Configuration via: yml
Modules: each module has its own independent war deployed
Requirement: Each module must log in a separated file
In the yml file I added the following lines to set the log output to a different file than the standar server.log:
logging:
file: ${SPF_LOGGING_PATH:app-logs/}log-#project.artifactId#-app.log
pattern:
console: "%d %-5level %logger : %msg%n"
file: "%d %-5level [%thread] %logger : %msg%n"
Using this configuration, a file in the output dir is created but no text is been written. So, adding this lines to the logging subsystem section in standalone.xml file, it started to write text from each module:
<add-logging-api-dependencies value="false"/>
<use-deployment-logging-config value="false"/>
Hope this may help anyone facing the same issue.
Cheers!

hibernate logback sql

I want to see the actual parameters of my SQL queries when I use Hibernate. I add this to my logback.xml to see the queries (with question marks):
<logger name="org.hibernate.type" level="TRACE" />
but to no effect.
Is there any special configuration necessary?
OnConsoleStatusListener shows me the correct configuration
23:48:15,246 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.type] to TRACE
but no output from org.hibernate.type package.
I'm using Spring with Jpa.
Things you have to make sure:
Are you sure that SLF4J + LogBack is working in your app?
Is your logger pointing to any appender?
Have you configured an appended?
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<!-- "application-name" is a variable -->
<File>c:/logs/${application-name}.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d %p %t %c - %m%n</Pattern>
</layout>
</appender>
<root level="debug">
<appender-ref ref="FILE"/>
</root>
</configuration>
I'm using this configuration, and it works for me:
<logger name="org.hibernate.type" level="trace" additivity="false">
<appender-ref ref="consoleAppender" />
</logger>
The logger that works for me is the following:
<logger name="org.hibernate.type" level="TRACE" />

Resources