Springboot Logging Pattern For Rolling Logs - spring-boot

I am using Springboot version 2.7 and trying to configure the log pattern to be daily rolling.
I am currently using just the application properties file to configure the logging as that's the preference.
I added the following line in the properties file but does not seem to work
logging.logback.rollingpolicy.file-name-pattern=myservice-%d{yyyy-MM-dd}.log
Any clues what I may be missing?
Also, is there a way to check daily log rolling without having to wait for EOD :)

First, you have to specify the file name:
logging.file.name=myservice.log
then you can use the rolling file name pattern
logging.logback.rollingpolicy.file-name-pattern=myservice-%d{yyyy-MM-dd}.log
To force the file change you could set the size to something small
logging.logback.rollingpolicy.max-file-size=100K
To specify the directory you must set this property
logging.file.path=/var/logs
The documentation can be found here:
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.logging

Related

Best way to concatenate/collapse stacktraces in logs

My goal is to include my stacktrace and log message into a single log message for my Spring Boot applications. The problem I'm running into is each line of the stacktrace is a separate log message. I want to be able to search my logs for a log level of ERROR and find the log message with the stacktrace. I've found two solutions but not sure which to use.
I can use Logback to put them all in one line but would like to keep the new lines for a pretty format. Also the guide I found might override defaults that I want to keep. https://fabianlee.org/2018/03/09/java-collapsing-multiline-stack-traces-into-a-single-log-event-using-spring-backed-by-logback-or-log4j2/
I could also use ECS and concatenate it there, but it could affect other logs (though I think we only have Java apps). https://docs.aws.amazon.com/AmazonECS/latest/developerguide/firelens-concatanate-multiline.html
Which would be the best way to do it? Also is there a better way to do it in Spring compared to the guide that I found?

Google Cloud Logs Export Names

Is there a way to configure the names of the files exported from Logging?
Currently the file exported includes colons. This are invalid characters as a path element in hadoop, so PySpark for instance cannot read these files. Obviously the easy solution is to rename the files, but this interferes with syncing.
Is there a way to configure the names or change them to no include colons? Any other solutions are appreciated. Thanks!
https://github.com/apache/hadoop/blob/trunk/hadoop-common-project/hadoop-common/src/site/markdown/filesystem/introduction.md
At this time, there is no way to change the naming convention when exporting log files as this process is automated on the backend.
If you would like to request to have this feature available in GCP, I would suggest creating a PIT. This page allows you to report bugs and request new features to be implemented within GCP.

Daily rolling log file in Websphere Liberty (16.0.0.4-WS-LIBERTY-CORE)

How to create a daily rolling log file in Websphere Liberty? I want the name of the log file to have YYYYMMDD format.
Currently I'm only able to limit the max file size, max file and a static naming of messages.log and disable consolelog.
<logging consoleLogLevel="OFF" maxFileSize="1" maxFiles="3" messageFileName="loggingMessages.log"/>
https://www.ibm.com/support/knowledgecenter/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ae/rwlp_logging.html
WebSphere Liberty does not currently have the ability to schedule log file rotation like traditional WAS. You can request this feature using the RFE site.
Alternatively, you could use an approach like Bruce mentioned - perhaps using a cron job to restart the server at midnight.
You might also consider configuring Liberty's binary logging. This will create a binary log file that can be queried to produce actual log files (with filtering options, etc.). It does have some time-based options. More info here.
Hope this helps, Andy
Probably not the answer you want, but if you restart the server it will roll the log.

Laravel logging treshhold

I am configuring monolog logging in laravel and I am wondering if there is a way to specify threshold for log file? For example in log4php you have maxFileSize property. Is there some way to do it with monolog?
(Check this for how to configure custom monolog: Configuring Custom Logging in Laravel 5 )
According to documentation laravel supports out of box only single, daily, syslog and error log logging modes.
I am wondering if is there way to use something between single and daily? I do not want to have daily log files and also do not like idea to have one big file. I would like to have possibility to specify threshold. For example 20 Mb and when this size is reached then to create new log file.
Does anybody has solution for that?
Use a proper log rotation facility that is available in your OS of choice for that.
On linux machine - logrotate
On Mac OS X - newsyslog
RotatingFileHandler in the monolog package, which Laravel uses for logging, intended to be just as a workaround.
Stores logs to files that are rotated every day and a limited number of files are kept.
This rotation is only intended to be used as a workaround. Using logrotate to
handle the rotation is strongly encouraged when you can use it.

Change trace file in H2

My application uses H2 but already has a log file (ex: abc.log)
Now, I'm trying to make even the H2 to write logs/errors to that file (abc.log) so if something goes wrong an user has only 1 file to send to me (not abc.log AND abc.db.trace file)
Is there a way to achieve that?
You can configure H2 to use SL4FJ as follows:
jdbc:h2:~/test;TRACE_LEVEL_FILE=4
The logger name is h2database.
Ok the solution was to simple for me to believe it but the only thing I had to do is to add
slf4j-api-1.7.2.jar
and
slf4j-jdk14-1.7.2.jar
in my app's classpath.
As SLF4J will (first search and then) discover by itself what underlying logging framework to use it is simply a matter of placing the right implementation.
One warning, it seems that SLF4J can not use more than one frameworks at a time so this solution work ONLY if you have a single existing framework.

Resources