Logging file is empty - spring

In my pet spring boot app I got logging properties like:
logging.file.name=Y:/Logs/app.log
logging.level.com.example.demo=debug
logging.level.org.springframework=info
logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} - %msg%n
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%
File app.log is created , but its emply, besides console log is works and gives all logs messadges, what's I do wrong?

Related

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

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>

Create new Spring log file every day

I want to configure Spring to write the log messages into file:
logging.file=/my-logs/app.log
logging.path=/my-logs/spring.log
Is it possible to rotate the file every day? I want to create a new file every day.
From the doc:
Log files rotate when they reach 10 MB and, as with console output, ERROR-level, WARN-level, and INFO-level messages are logged by default. Size limits can be changed using the logging.file.max-size property. Previously rotated files are archived indefinitely unless the logging.file.max-history property has been set.
Also, if you just want to log to "/my-logs/app.log", delete logging.path and change logging.file to:
logging.file=/my-logs/app.log
Edit: about rotating the log every day, Spring default logger does not support it, you can use Logback. Create a file called logback-spring.xml in src/main/resources with the following content:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}[%M:%L] - %msg%n</pattern>
</encoder>
</appender>
<appender name="ROTATE_FILE_DAILY" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/my-logs/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>app-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}[%M:%L] - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ROTATE_FILE_DAILY"/>
</root></configuration>

How to print logs for 2 classes in 2 different files in spring boot

I have set logging.file=C:/usr/local/tomcat/logs/hib.log in application.properties and setting like below in class
private static final Logger logger = LogManager.getLogger(ChargeMasterController.class);
logger.info("Total time taken for is ---------------------------" + time + " ms");
Logs are getting printed fine in the path mentioned as logging.file.
Now I want to print my logs in 2 different files for 2 different classes(In same package), how can I set 2 logging.file in application.properties
You can't do that with the configuration Spring provides. But since spring-boot-starter-logging uses Logback by default, you can use a Logback configuration in stead and define multiple appenders, for example:
<appender name="FILE1" class="ch.qos.logback.core.FileAppender">
<file>log1.log</file>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<appender name="FILE2" class="ch.qos.logback.core.FileAppender">
<file>log2.log</file>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<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>
After that, you could use the FILE1 appender for your first class, FILE2 appender for your second class, and STDOUT for all other classes. This can be done by defining the loggers:
<logger name="com.example.pkg.ClassName1" additivity="false" level="info">
<appender-ref ref="FILE1" />
</logger>
<logger name="com.example.pkg.ClassName2" additivity="false" level="info">
<appender-ref ref="FILE2" />
</logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
As you can see, there are two loggers next to the root logger, called com.example.pkg.ClassName1 and com.example.pkg.ClassName2. These should match the names of the classes that should log to separate files.
The additivity="false" is important to make sure that logs that go to log1.log or log2.log aren't also sent to the console since they would also match the root logger.
This configuration can be placed within a logback.xml file on your classpath (eg. src/main/resources). Alternatively, you can also configure the location by using the logging.config property within application.properties. For example:
logging.config=file:/path/to/logback.conf
Or if you want to use an arbitrary location on your classpath:
logging.config=classpath:my/logback.conf

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

Filter logs in spring-boot application

In application.yml of my spring-boot application, I have the following code:
logging:
file: test.log
pattern:
file: "%d %-5level [%thread] %logger : %msg%n"
I would write something like
logging:
level:
org: hibernate: *
I would to filter and gather all log messages of all levels (ERROR, DEBUG, INFO, etc) ifrom only one specific package (org.hibernate) in "test.log". It is possible?
Yes, you can define log level for each of your/third library packages:
Example for application.yml:
# Fine-tuning a specific logger (for a single class)
logging:
level:
yourpackage: INFO
org:
hibernate: ALL
springframework: INFO
Example for application.properties:
# Fine-tuning a specific logger (for a single class)
logging.level.yourpackage.controllers.HomeController = INFO
# Specify the level for spring boot and hibernate's loggers
logging.level.org.springframework = INFO
logging.level.org.hibernate = ALL
Update 1: Only logging hibernate.* to a log file
If you want to log only one specific Java package to your log file add a logback.xml to your src/main/resources folder and add the following settings:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>hibernateLogs.log</file>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<logger name="org.hibernate" level="ALL">
<appender-ref ref="FILE" />
</logger>
</configuration>
This config will log all of your hibernate logs to a file.

Resources