Springboot how to rotate log files on the server restart - java

Springboot how to rotate log files on the server restart.
I have the below entries
# LOGGING
logging.level.org.springframework.web=WARN
logging.level.org.hibernate=WARN
logging.file=/var/log/apps/myapp.log
I can't find any details here:
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/common-application-properties.html

As per the following link in application.properties regarding log files, the following are the configurations.
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup.
logging.file.max-size=10MB # Maximum log file size. Only supported with the default logback setup.
Apart from the above, you can also check below the configuration based upon the server configured.
server.tomcat.accesslog.rotate=true # Whether to enable access log rotation.
server.undertow.accesslog.rotate=true # Whether to enable access log rotation.
I would suggest to use Slf4j along with logback. You need to configure logback.xml and you can configure rolling file appender.

You can add the custom log configuration with logback-spring.xml.
The various logging systems can be activated by including the appropriate libraries on the classpath and can be further customized by providing a suitable configuration file in the root of the classpath or in a location specified by the following Spring Environment property: logging.config.
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-configuration

I got a rotation policy with setting a limit to the log file size and the same for the backup.
logging.file.name=/path/log/file
logging.file.max-size=10MB
logging.file.total-size-cap=10MB
IMPORTANT: it will keep just a backup file.

I dont know your exact scenario but you can do this by passing env_variable when you start a server.. like your build version number and use that in your property file
spring-boot:run -Dbuild.number=1.x
And you may use this like
# LOGGING
logging.level.org.springframework.web=WARN
logging.level.org.hibernate=WARN
logging.file=/var/log/apps/myapp-${build.number}.log
Dont forgot to increment number everytime.

Related

Not able to print logs to Tomcat's CATALINA_BASE by using log4j2

I have update our code from log4j to log4j 2.17.1 And I want to stored the log file to servers under the Apache tomcat. I am using the log4j2.properties mentioned below.
When I run the code, then the logs file is printed in under code structure(see in below attached screenshot)but I want to print the logs file in QA-Servers under apache tomcat.
Please help me to solve the issue.
TL;DR: use ${sys:catalina.base}.
The property substitution in Log4j 2.x differs from Log4j 1.x (cf. documentation). The most prominent change is that:
in Log4j 1.x ${catalina.base} is looked up in Java system properties and, if the system property does not exist, in the configuration file,
in Log4j 2.x ${catalina.base} is looked up only in the configuration file.
In both cases if the property can not be resolved the placeholder is left unchanged.
In Log4j 2.x all external property lookups must be prefixed using an appropriate prefix. The exact equivalent of Log4j 1.x behavior is ${sys:catalina.base}. Therefore you can use:
# Fallback
property.catalina.base=.
appender.rolling.fileName=${sys:catalina.base}/logs/aseq_wiptmobile_qa-1.applog

How generate log files separated by apps running over websphere 9 ? I use java.util.logging to generate logs

I use websphere 9 application server to deploy war's and ear's, and use java.util.logging to generate logs into applications. I try to use properties file to configure the FileHandler of the LogManager, but websphere write ALL other logs on my file.
I not use log4j because i can't set log levels at runtime.
Is possible make differents file logs by application over websphere with java.util.logging ?
This is my properties file Logger.properties
handlers= java.util.logging.FileHandler
#java.util.logging.ConsoleHandler.level = INFO
#java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# Set the default formatter to be the simple formatter
java.util.logging.FileHandler.formatter =java.util.logging.SimpleFormatter
# Use UTF-8 encoding
java.util.logging.FileHandler.encoding = UTF-8
# Write the log files to some file pattern
java.util.logging.FileHandler.pattern = C:/Users/pmendez/Documents/Log/testLogs.log
# Limit log file size to 5 Kb
java.util.logging.FileHandler.limit = 5000
# Keep 10 log files
java.util.logging.FileHandler.count = 10
#Customize the SimpleFormatter output format
java.util.logging.SimpleFormatter.format = %d [%t] %-5p (%F:%L) - %m%n
I try to use properties file to configure the FileHandler of the LogManager, but websphere write ALL other logs on my file.
Per your logging.properties you are attaching the FileHandler to the root logger. It would be expected to see log records from WebSphere because by default Logger::setUseParentHandlers is set to true.
Is possible make differents file logs by application over websphere with java.util.logging ?
You have to do one of the following:
Attach your FileHandler to the root logger of your application so the FileHandler is not attached to parent of the WebSphere application. Say application package is com.my.app.rules you can add entry of tocom.my.app.rules.handlers=java.util.logging.FileHandler and remove the attachment to the root logger. Next you demand the logger from code and pin it to memory so this package becomes the new root logger of your application code.
Create java.util.logging.Filter and install it on the FileHandler.
Turn off the logging for WebSphere. You can do this by setting the root logger to off and forcing all of your loggers to say info.
.level=OFF
com.my.app.rules.class1.level=INFO
com.my.app.rules.class2.level=INFO
com.my.app.rules.class3.level=INFO
It's possible set one FileHandler by app ?
The default java.util.logging.LogManager only supports one set of FileHandler settings. You can attach instances to various loggers but it doesn't allow for instances with different settings via the logging.properties. However, the LogManager does support per the documentation:
A property "config". This property is intended to allow arbitrary configuration code to be run. The property defines a whitespace or comma separated list of class names. A new instance will be created for each named class. The default constructor of each class may execute arbitrary code to update the logging configuration, such as setting logger levels, adding handlers, adding filters, etc.
So what you need to do is bundle a configuration class with your application that performs the needed FileHandler and logger configuration and modify your logging.properties to load it. One issue with this approach is that the LogManager can only see classes from the system class loader.
From jmehrens answer you can see it might be a bit difficult. Maybe you should consider switching logging to High Performance Extensible Logging (HPEL). It is just setting in the application server, so no changes in the applications code.
Then you could query logs for the given application using command line tool:
logViewer.sh -includeExtensions appName=PlantsByWebSphere
This would be much easier, if it fits your purpose. Moreover, generating separate log files is now not recommended, if you plan to move your apps in the future into containers/cloud or refactor them into microservices.
My solution to this isue was create a PersonalFileHandler to use on all Logger's. Properties are read from properties file called "Logger.properties", and invoque read this programmatically, same like this:
//Read config file
LogManager.getLogManager().readConfiguration(LoggerJUL.class.getResourceAsStream("/Logger.properties"));
//Add handler to logger
Logger.getLogger(clazz)).addHandler(new PersonalFileHandler());
PersonalFileHandler extends FileHandler, and properties are set by configure method on FileHandler class on runtime.

How to set different log levels in Eclipse Scout framework?

I'm having some trouble configuring proper logging in Eclipse Scout framework. My claims aren't that high as I only want to be able to set different log levels for different parts of my program in a configuration/properties/XML file.
The logging configuration in the config.ini of my Scout server plugin currently looks like this:
eclipse.consoleLog=true
org.eclipse.scout.log=eclipse
org.eclipse.scout.log.level=INFO
So as you can see this is the default logging configuration using Eclipse logging. It works fine for logging at a global level. The only thing I would like to do is to write something like this to set the different log levels:
packagename.ClassName=LOGLEVEL
As this is a very basic logging use case I think there must be some easy way to do this in Scout. Otherwise I would appreciate some help how to configure log4j, JUL or others for the use with Scout. The Eclipse Scout Wiki hasn't helped me so far. I created the example logger fragment to the host plugin 'org.eclipse.scout.commons' and removed the logging configuration lines from my config.ini but nothing happens. I'm also not sure where to put the log4j.poperties or how this is done otherwise.
I'm a bit ashamed for being unable to figure out such a basic problem, but would be very happy about some quick help.
I can tell you how to configure the logging if you choose the java logger (config.ini: org.eclipse.scout.log=java).
For the eclipse logger, I barely found any information at all.
Now, to configure the java (JUL) logging: You can do this in a file called logging.properties.
You can configure the logging by specifying the configuration file in your product:
Create your configuration file - say logging.properties inside the folder where your product file (for server or client respectively) is located. Typically this is in a folder named 'products'.
Open your product file and go to the "Launching" tab and specify your logging configuration file in the "VM Arguments" tab. Use the "java.util.logging.config.file" system property to do so:
-Djava.util.logging.config.file="${resource_loc:/com.yourapp.server/products/logging.properties}"
Now, you should be able to specify the log levels in your new logging.properties file:
### Root level of your application, all below are ignored
.level=INFO
### Handlers
handlers=java.util.logging.ConsoleHandler
### Handler properties
java.util.logging.ConsoleHandler.level=FINEST
### Override the logging level for certain classes
com.yourapp.server.SomeService.level=FINE
Alternatively, you can also use a class to initialize the logging with the java.util.logging.config.class option. See this wiki page for a detailed example.
Also, when building a WAR file, you might be interested in this answer.

How to use multiple configuration files for Log4j2?

I am writing Java code that tests a Java library. The library includes its own Log4j2 configuration as part of the distribution.
I would like to use Log4j2 in my test code without modifying the library's configuration.
Is there a way to have a separate Log4j2 configuration for my test code?
This is all running as command-line Java, no servers or web involvement at all.
EDIT
What I want is to be able to configure loggers, appenders, etc for the test code to use, and at the same time have the library code use its own separate configuration file for its logging.
The idea is to use Log4j2 in my test code, but without having to change the library's configuration file. Since the library configuration file is part of the library's distribution, I don't want to change it for testing.
This may be helpful:
Log4j2 will first look for log4j2-test.xml in the classpath
if that file is not found, it will look for log4j2.xml in the classpath
So one option is to copy the library's configuration (log4j2.xml) to log4j2-test.xml and add your own configuration to log4j2-test.xml.
Furthermore, Log4j2 supports XInclude in XML configuration, so you could use that feature to avoid duplicating the library's configuration in your log4j2-test.xml.
Log4j2 supports "Composite Configuration" which exactly matches your requirement. All you need to do is provide path to multiple files in log4j.configurationFile property. This can be passed from command line or added to log4j2.component.properties file in your application.
References
https://logging.apache.org/log4j/2.x/manual/configuration.html#CompositeConfiguration
https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties
There are two step you can try to solve for your issue
Create your own configuration file with your custom name(eg: xyz.properties/.xml)
You must add the following line to your java runtime command
cmd> java -Dlog4j.configuration=location/xyz.properties
If you use diffent name for configuration rather log4j.properties/.xml file you need to configure that file at runtime by above command for more info have a look here..
Correct format for using an alternate XML file to log4j2.xml:
java -Dlog4j.configurationFile=./location/log4j2-custom.xml
Assuming ./location/log4j2-custom.xml exists and is the new XML to replace log4j2.xml in this run
See:
https://github.com/kamalcph/Log4j2Examples/blob/master/src/main/java/in/co/nmsworks/log4j2/examples/CompositeConfigurationExample.java
Referencing https://logging.apache.org/log4j/2.x/manual/configuration.html states that you can add multiple comma separated files under log4j2.configurationFile property.
To use multiple configuration files, depending on the environment you must set the.
for example:
if (env.equals("DEV")) {
setConfigFile("log4j2-dev.xml");
}
public static void setConfigFile(String logConfigFile) {
File file = new File(logConfigFile);
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
context.setConfigLocation(file.toURI());
}
first configure application.yaml file
spring:
profiles:
active: dev
---
spring:
message: running in the dev profile //just to output the message in the log
profiles: dev
logging.config: classpath:log4j2-dev.xml
---
spring:
profiles: prod
logging.config: classpath:log4j2-prod.xml
and create these similar files in your classpath
* log4j2-dev.xml
* log4j2-prod.xml

Hibernate doesn't log anything anymore, since I relocated the log4j.xml and hibernate.properties files

I had the following working organization
src/main/resources/log4j.xml
src/main/resources/hibernate.properties
I wanted to reorganize my webapp as follow:
src/main/resources/log/log4j.xml
src/main/resources/orm/hibernate.properties
The Logger.info("foobar") still logs well (after having set the log4jConfigLocation context parameter), and the app still has a working database connection.
The problem is that Hibernate doesn't log anything anymore, even if hibernate.show_sql is set to true. Any idea why? Should specify the new path to the log4j.xml file to Hibernate? If yes, how?
You could run your server with this VM argument:-
-Dlog4j.configuration=/path/to/log4j.xml
I usually tend to place the log4j.xml at the recommended default location unless there's a need to do otherwise... "convention over configuration" is important especially if other peers may be working on the same project in the future.
Log4j first looks for its configuration by looking at the system property "log4j.configuration". If that system property is not set, it looks for a log4j.properties or a log4j.xml file on the classpath.
So, if you really want the log4j.xml at src/main/resources/log/log4j.xml, you will have to set the log4j configuration system property. This is basically what limc does by supplying it as a vm argument.
Also like limc says, you should probably just keep the log4j.xml at the default location.

Categories