Is it compatible rsyslog with Logback-SLF4J? - spring-boot

I'm using spring-boot-starter-web like framework. These, use Logback (without implement other libraries) to manage logs with SLF4J like a facade. I need to use it with rsyslog but the official doc refers only to Syslog.
I tried to use the Syslog implementation since Syslog inherit for rsyslog but not found ( i attach my logback-spring.xml below ).
Any idea?
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<property resource="application.properties" />
<!-- Syslog , make sure that syslog are enabled in the OS-->
<appender name="RSYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
<syslogHost>127.0.0.1</syslogHost>
<facility>LOCAL0</facility>
<port>514</port>
<throwableExcluded>true</throwableExcluded>
<suffixPattern>%package.yes.rest %m thread:%t priority:%p category:%c
exception:%exception:%msg</suffixPattern>
</appender>
<logger name="package.yes.rest" level="info" additivity="false">
<appender-ref ref="RSYSLOG"/>
</logger>
</configuration>
Bonus clip: I see the choose of change Logback to Log4j2 too, but it's more stable use the inherit Logback.

Related

How to change log level in a spring boot project(in production enviroment) by change any property in application properties if I am using log4j2

I am developing a spring boot service (2.1.7 spring boot version) where I am using log4j2 to establish the logs and their pattern:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" name="MyService">
<Properties>
<Property name="project.component">${bundle:bootstrap:project.component}</Property>
<Property name="project.version">${bundle:bootstrap:project.version}</Property>
</Properties>
<Appenders>
<Console name="main" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss,SSSZ} ${project.component} ${project.version} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="root" level="INFO">
<AppenderRef ref="main" level="INFO"/>
</Logger>
</Loggers>
</Configuration>
I am not using log4j2.properties.
My problem is that I am only able to change the log level by log4j2.xml, I have tried with several spring boot properties in my application.properties as logging.level.root or through actuator endpoints but they did not work. When I am in a production environment and I need to change the logs level I have not a way to do it.
Any suggestion?
I am able to change the log level using a similar configuration, which even let's you configure a pattern for the log:
# Set everything to be logged to the console
log4j.rootCategory=TRACE, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n
Per-class log levels can also be set, in example:
# Settings to quiet third party logs that are too verbose
log4j.logger.org.eclipse.jetty=WARN
log4j.logger.org.eclipse.jetty.util.component.AbstractLifeCycle=ERROR
log4j.logger.org.apache.spark.repl.SparkIMain$exprTyper=INFO
log4j.logger.org.apache.spark.repl.SparkILoop$SparkILoopInterpreter=INF
log4j.logger.org.apache.spark.sql.execution.streaming.FileStreamSource=TRACE

Spring boot Microservices logging with Graylog

I want to use Graylog/RabbitMQ for logging with my spring boot microservices. As per my understanding I have to send my logs to RabbitMQ and have to integrate it with Graylog. I want to know the workflow and how to implement it like how to send the logs to RabbitMQ, do I need to use any other logging framework.
You can use Logback appender to send logs from spring-boot app. Add following dependency to your pom.xml
<dependency>
<groupId>de.siegmar</groupId>
<artifactId>logback-gelf</artifactId>
<version>1.1.0</version>
</dependency>
Then you need to add a logback configuration file to your classpath.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<appender name="GELF" class="de.siegmar.logbackgelf.GelfUdpAppender">
<graylogHost>localhost</graylogHost>
<graylogPort>12201</graylogPort>
<maxChunkSize>508</maxChunkSize>
<useCompression>true</useCompression>
<layout class="de.siegmar.logbackgelf.GelfLayout">
<originHost>localhost</originHost>
<includeRawMessage>false</includeRawMessage>
<includeMarker>true</includeMarker>
<includeMdcData>true</includeMdcData>
<includeCallerData>false</includeCallerData>
<includeRootCauseData>false</includeRootCauseData>
<includeLevelName>false</includeLevelName>
<shortPatternLayout class="ch.qos.logback.classic.PatternLayout">
<pattern>%m%nopex</pattern>
</shortPatternLayout>
<fullPatternLayout class="ch.qos.logback.classic.PatternLayout">
<pattern>%m</pattern>
</fullPatternLayout>
<staticField>app_name:backend</staticField>
<staticField>os_arch:${os.arch}</staticField>
<staticField>os_name:${os.name}</staticField>
<staticField>os_version:${os.version}</staticField>
</layout>
</appender>
<root level="debug">
<appender-ref ref="GELF" />
<appender-ref ref="CONSOLE" />
</root>
</configuration>
For more information: logback-gelf

How to set max number of archived logs in spring boot

I tried following settings in application.properties:
logging.file=foo/bar.log
logging.file.max-history=2
logging.file.max-size=1KB
Still, its not limiting the number of archive logs to 2.
As per application properties documentation reference, only supported when you setup logback.
logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup.
So to add support of logback please see section 79.1 Configure Logback for Logging & 79.1.1 Configure Logback for File-only Output of Spring Boot Logging Guide
If you want to disable console logging and write output only to a
file, you need a custom logback-spring.xml that imports
file-appender.xml but not console-appender.xml, as shown in the
following example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>

Spring and Log4j ver 2 - XML configuration example

I am trying to configure Log4j 2 in Spring XML configuration file for the first time (but unsuccessfully). I need to create two appenders - one for logging into console (>=DEBUG) and another for logging into database via JDBCAppender (>= INFO).
There is a problem because I don't know how to set another log level logger that differs from root logger.
Thank you for sharing some XML configuration sample. Thanks in advance!
You can set the level on the appender ref.
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<appender name="A">
...
</appender>
<appender name="B">
...
</appender>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="A" level="info"/>
<appender-ref ref="B" level="debug"/>
</root>
</loggers>
</configuration>

How to enable logging of Ehcache

In my Spring + Hibernate project, I was doing logging by SLF4J 1.6.4 with LogBack. Now, I've added Ehcache 2.2.0 (through ehcache-spring-annotations-1.1.3). The caching seems to be working as the method, annotated with #Cacheable, no longer being executed, though returning the correct result. But, I'm interested to see the log written by the Ehcache. As Ehcache also uses SLF4J, I supposed, the log should be written into my log file. But, this is not happening. The logback.xml has the following.
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ROLLING"/>
</root>
Adding following also doesn't help
<logger name="net.sf.ehcache">
</logger>
Ehcache.xml
<cache name="sampleCache1"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
/>
Please advise me to overcome the problem.
The Ehcache is using SLF4J 1.6.1, while my project was using SLF4J 1.6.4. Can it cause any problem?
Thanks
EhCache logs a lot on DEBUG level. First of all, this configuration snippet filters out all logging statements below INFO:
<root level="info">
Change it to
<root level="ALL">
Secondly
<logger name="net.sf.ehcache">
needs an increased logging level
<logger name="net.sf.ehcache" level="ALL"/>
You should then see plenty of logging statements from EhCache (and others).
In my opinion setting the root logger level to ALL is not a good idea.
This means that ALL log messages of every level will get through (unless you set a threshold on your appenders).
Instead, I suggest you set a sensible root logger level, such as INFO or WARN, and then set the log level of individual loggers if you require more specific information.
This way you are not creating a forest of unnecessary information.
I suggest something like the below:
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<logger name="net.sf.ehcache">
<level value="DEBUG"/>
</logger>
<root>
<priority value ="INFO" />
<appender-ref ref="CONSOLE" />
</root>
I also found it handy to enable DEBUG logging on org.springframework.cache because I'm using Spring Framework 4.2.1's caching feature (#Cacheable etc.).
I wrote a custom logger for Ehcache 3 by implementing the CacheEventListener interface as follows
public class CacheLogger implements CacheEventListener<Object, Object> {
private static final Logger LOG = LoggerFactory.getLogger(CacheLogger.class);
#Override
public void onEvent(CacheEvent<?, ?> cacheEvent) {
LOG.info("YOUR LOG IS HERE");
}
}
The ehcache.xml will define the listener class
<cache-template name="default">
<expiry>
<ttl unit="seconds">300</ttl>
</expiry>
<listeners>
<listener>
<class>com.path.to.CacheLogger</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
<events-to-fire-on>EVICTED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap>1000</heap>
<offheap unit="MB">10</offheap>
</resources>
</cache-template>

Resources