How to ignore logback RollingFileAppender FileNotFoundException - spring-boot

I am using logback in my spring boot project, but logback log file must locate in /home/xxx/logs folder.
spring boot cannot start caused by RollingFileAppender's FileNotFoundException exception in my MacOS machine, because of MacOS cannot create folder /home/xxx/logs.
How to ignore this Exception in my spring boot?

As much I know logging framework don't provide any exception handling capability. It's not their job. Either you Correct log location and syntax or just remove config which you don't need.

Include a file logback-spring.xml in your classpath or resources folder and then you will be able to explicitly configure the location of the log file or you may have it print to the console instead. The config below would do just that and override the default config that comes with spring boot.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>

Related

Spring Boot YML configuration for Log output in JSON

I am trying to get log output in JSON format. I achieved it by configuring logback.xml file. Is it possible to achieve same by YML file ?
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>true</prettyPrint>
</jsonFormatter>
<timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
</layout>
</appender>
If your goal is to have different configurations for different environments, you can use spring's profile dependent configuration feature
From my logback-spring.xml:
...
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<springProfile name="dev">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</springProfile>
<springProfile name="qa,prod">
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
</springProfile>
</appender>
...
You should also check the paragraph about the Environment Properties
As mentioned above in comment It is not possible to configure only in YML file for JSON Layout in logback. My requirement was to set log level ERROR/INFO from YML file. I came to know We can override logback log level by YML file in spring boot. For JSON format configuration one can use logback.xml and for setting log level use YML file

Print only application logs

I want to print my logs of Spring Boot application, but I want to print my application logs in INFO. It is writing everything from server startup to stop.
I want to write logs in INFO, that I wrote into my application.
How to do it in Spring Boot?
Disable startup log messages by envoking .logStartupInfo(false) in your Application builder.
It should look like:
new SpringApplicationBuilder(ServiceConfiguration.class)
.logStartupInfo(false)
.run(args);
Please refer to this link for more clarity.
OR
Add logback.xml inside src/main/resources and the contents should be:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n</pattern>
</encoder>
</appender>
<root level="off">
<appender-ref ref="STDOUT" />
</root>
</configuration>
For more information please take a look at this link

Spring boot logging into mysql database

I have to put all the log data (i.e., debug, info, error) into mysql database instead of to file/console.
I read the spring boot documentation but I didn't see any configuration related to database for logging.
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
Also tried the following link but its also not working.
https://www.tutorialspoint.com/log4j/log4j_logging_database.htm
Can anyone help me to do this.
Thanks.
I read the spring boot documentation but I didn't see any
configuration related to database for logging.
Because spring boot hands off that functionality to logging framework (slf4j + logback/log4j etc). So you need to configure your logging framework accordingly using it's configuration file (eg: logback.xml, logback-spring.xml, logback.groovy etc). Default logging frameworks in Spring boot is slf4j+logback. So checkout how you can use DBAppender.
For Logback
https://logback.qos.ch/manual/appenders.html#DBAppender
http://learningviacode.blogspot.com/2014/01/writing-logs-to-database.html
Log to database with LogBack
https://medium.com/#chakrar27/storing-log-data-in-postgresql-using-logback-db-appender-292891a9918
1. create logback.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<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>
</appender>
<appender name="db" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource
class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<driverClass>org.postgresql.Driver</driverClass>
<url>jdbc:postgresql://localhost:5432/simple</url>
<user>postgres</user>
<password>root</password> <!-- no password -->
</connectionSource>
</appender>
<!-- the level of the root level is set to DEBUG by default. -->
<root level="TRACE">
<appender-ref ref="stdout" />
<appender-ref ref="db" />
</root>
</configuration>
2. Create the 3 tables
logging_event
logging_event_property
logging_event_exception
They must exist before DBAppender can be used
For Log4J
https://logging.apache.org/log4j/2.x/manual/appenders.html#JDBCAppender
For Log4J2
http://smasue.github.io/log4j2-spring-database-appender

How to implement logging in custom module in Spring XD?

It's not so easy to debug custom module deployed to the Spring XD runtime (version 1.3.1-RELEASE).
I'm aware of log sink, however it's something different that I want to achieve.
I'd like to add my own log messages to the XD log (ideally to the STDOUT alongside it's own logs). These log messages are generated in my custom module (processor in this case) using slf4j API.
I've added:
org.slf4j.Logger#info invocation to the processor class
logback-classic dependency to the pom.xml (w/o a version, as it's managed by spring-xd-module-parent dependencyManagement
logback.xml to the resources directory
logback-test.xml to the test resources directory
Log messages are logged into STDOUT during integration test invocation (via SingleNodeIntegrationTestSupport), however they don't appear in the XD log when module is uploaded or stream using it is deployed.
logback.xml contents (identical for -test):
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<logger name="com.maxromanovsky" level="debug" />
<logger name="org.springframework" level="warn" />
<logger name="org.apache.zookeeper" level="error" />
<root level="warn">
<appender-ref ref="STDOUT" />
</root>
The container logback configuration files can be found in xd/config (xd-container-logback.groovy and xd-singlenode-logback.groovy).
You need to add your custom logger configuration there.

Springboot sending logs to fluentd not working

I need some help for the following problem.
I have a spring boot application and I would like to configure a fluentd appender using logback.
I've created a file called logback.xml in my src/main/resources with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date - %level - [%thread] - %logger - [%file:%line] - %msg%n</pattern>
</encoder>
</appender>
<appender name="FLUENT_TEXT" class="ch.qos.logback.more.appenders.DataFluentAppender">
<tag>dab</tag>
<label>normal</label>
<remoteHost>localhost</remoteHost>
<port>24224</port>
<maxQueueSize>20</maxQueueSize>
</appender>
<logger name="org.com" level="DEBUG"/>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FLUENT_TEXT" />
</root>
</configuration>
In my build.gradle I have :
compile 'org.fluentd:fluent-logger:0.3.1'
compile 'com.sndyuk:logback-more-appenders:1.1.0'
When I launch the app using gradle bootRun I have the following message:
10:56:33,020 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - Attempted to append to non started appender [STDOUT].
10:56:33,020 |-WARN in ch.qos.logback.more.appenders.DataFluentAppender[FLUENT_TEXT] - Attempted to append to non started appender [FLUENT_TEXT].
10:56:33,028 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - Attempted to append to non started appender [STDOUT].
Exception in thread "main" 10:56:33,028 |-WARN in ch.qos.logback.more.appenders.DataFluentAppender[FLUENT_TEXT] - Attempted to append to non started appender [FLUENT_TEXT].
java.lang.NullPointerException
at ch.qos.logback.more.appenders.DataFluentAppender$FluentDaemonAppender.close(DataFluentAppender.java:72)
I've found here https://github.com/spring-projects/spring-boot/blob/master/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc something saying that logback.xml is loaded too early so I need to use a file called logback-spring.xml.
I've did it and it's like the file is never loaded, no error but nothing gets to my fluetd socket.
Any idea how to solve it ?
Thanks.
C.C.
When running your springboot application, load a 'spring' profile.
One way of doing it would be via the command line, see below.
-Dspring.profiles.active=spring

Resources