How to print the logging directory of the spring-boot application in console while startup? - spring

How to enable the spring-boot application to print the logging directory while startup?, like below example:
logging directory - /home/test/studentapp.log
Irrespective of any logging framework we use like LOGBACK, Log4j2, Java Util logging ..e.t.c in Spring, want to print the logging directory while startup.
Is there any property needs to be configured at application.properties to enable this?
Edit:
For example, if I use the below logging logback.xml configuration, then I want to log the logging directory mentioned in the below configuration to the console at application startup, Like If you start kafka server It will give the location of the logging directories in the console at startup. In a similar way can we have this feature in spring?
<appender name="STUDENT_APP_LOG_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/home/test/studentapp.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%d{ dd-MM-yyyy HH:mm:ss.SSS }] [ %5p ] [%thread] [ %logger ] %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>/home/test/archived/studentapp-%d{dd-MM-yyyy HH:mm:ss}.%i.log</fileNamePattern>
<maxFileSize>100MB</maxFileSize>
<totalSizeCap>10GB</totalSizeCap>
<maxHistory>90</maxHistory>
</rollingPolicy>
</appender>

Related

How to ignore logback RollingFileAppender FileNotFoundException

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>

Logging pattern in spring boot application

How do I set the logging file pattern to something like server.log.2017-12-22.gz?
As of now in my application.properties file, I have set the logging pattern to:
logging.pattern.file= "%d{yyyy-MM-dd } [%thread] %-5level %logger{36} - %msg%n"
logging.file=/var/opt/VS_Logs/server.log
But I need to store the files in the following format: server.log.2017-12-22.gz
As soon as you want custom rolling and triggering policies, you can no longer rely on Spring boot's logging configuration, and you have to use the vendor specific logging configuration. Here is an example using Logback and a TimeBasedRollingPolicy:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>server.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>server.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd } [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
Logback will automatically gzip it when you use the .gz extension. If you save this file as logback.xml and put it on your classpath, Spring boot will automatically detect it, otherwise, you can use the logging.config property:
logging.config=classpath:logback.xml

How can I use YAML to configure multiple log files for Logback/Springboot?

I'm rewriting a small DropWizard application to run on SpringBoot.
My DW app has the following logging config which works:
logging:
level: INFO
appenders:
- type: file
currentLogFilename: /var/log/paas/console.log
archivedLogFilenamePattern: /var/log/paas/console.log-%d.gz
archivedFileCount: 7
loggers:
com.myorg:
level: DEBUG
appenders:
- type: file
currentLogFilename: /var/log/paas/paas.log
archivedLogFilenamePattern: /var/log/paas/paas.log-%d.gz
archivedFileCount: 7
This config separates my application and console messages into two separate logs.
When I try using this same configuration with SpringBoot, it has no effect. I'm able to write everything to a single log with the following config, but I really need to have two separate logs:
logging:
level:
org.springframework.web: INFO
com.myorg: DEBUG
file: /var/log/paas/paas.log
Is it not possible to do this with LogBack and YAML? Or is there an alternate syntax that will give me the same result I'm getting for my DropWizard app?
Spring Boot's YAML configuration of Logback only allows a single file appender. In order to configure Logback to use multiple file appenders you'll have to provide an explicit logback.xml or logback-spring.xml. If you remove the logging confoguraiotn from your application.yaml file and then add either a logback.xml or a logback-spring.xml to the root of your runtime classpath then Logback will be configured from that file.
Here's an example, using a logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- deny all events with a level below INFO, that is TRACE and DEBUG -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<file>/var/log/paas/console.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/var/log/paas/console.log-%d.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>...</pattern>
</encoder>
</appender>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/paas/paas.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/var/log/paas/paas.log-%d.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>...</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="DEBUG_FILE"/>
</root>
</configuration>

Enabling logs in Spring boot cloud foundry application

how do i enable spring framework logs in my application. ? i have used logback.xml in my application and set the root level to debug. When i am trying to run the app locally then logs are printed but the same is not happening when i am deploying the application in CF.
The application itself is crashing due to other reason but i hoped some initial spring boot framework logging should have happened.
Below is my logback.xml file. I am not sure the console appender mentioned there will work in CF system too or not.`
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<root level="TRACE">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Best Regards,
Saurav
May be this was a problem with the spring boot CF service broker application which was causing the logs to not be printed. Check here CF Spring boot app failed to start accepting connections
But then i deleted the application and redeployed again. It started printing logs. The above configuration works.
The above Logback configuration file should work. Note: TRACE level logging will produce a lot of log messages. It might be better to turn this down to INFO. You'll need to bundle this configuration file in src\main\resources\logback.xml in your Spring Boot app structure.
Application logs in PCF must be written to stdout or stderr by your app, and you can view them in the CLI using the command cf logs. The ConsoleAppender you're using above writes to stdout, so you should be good to go.

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.

Resources