JAVA_TOOL_OPTIONS variables in Logback configuration - aws-lambda

I have a small AWS Lambda function written in Java with a Logback attached to it for sending out error log emails. The variables needed for the Logback appender (basic SMTP details) are loaded into the Logback.xml configuration appender SMTPAppender in the usual format of "${smtpPort}" etc. The variables are maintained through the Lambda Function Environment variable: key=JAVA_TOOL_OPTIONS, value=-DsmtpPort=587 -DsmtpHost=xyz.
Here is the logback.xml configuration:
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{ISO8601} %-5p [%t] %c - %m%n</pattern>
</encoder>
</appender>
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpPort>${smtpPort}</smtpPort>
<STARTTLS>${startTls}</STARTTLS>
<smtpHost>${smtpHost}</smtpHost>
<username>${smtpUser}</username>
<password>${smtpPassword}</password>
<to>${logEmailTo}</to>
<from>xxxx#xxxxxxxx.net</from>
<subject>Error log [Scheduled Task Runner]</subject>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%date%thread%level%logger%msg</pattern>
<cssBuilder class="LambdaHandlers.util.ErrorEmailCssBuilder"/>
</layout>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
<!-- send just 10 log entries per email -->
<bufferSize>10</bufferSize>
</cyclicBufferTracker>
</appender>
<logger name="org.apache.commons.httpclient" level="OFF"/>
<logger name="httpclient.wire.header" level="OFF"/>
<logger name="httpclient.wire.content" level="OFF"/>
<logger name="httpclient.wire" level="OFF"/>
<logger name="org.apache.http" level="OFF"/>
<logger name="io.micrometer" level="INFO"/>
<logger name="software.amazon.awssdk" level="OFF"/>
<logger name="io.netty" level="OFF"/>
<logger name="org.apache.catalina.realm" level="DEBUG"/>
<logger name="org.apache.axis" level="INFO"/>
<root level="debug">
<appender-ref ref="STDOUT"/>
<appender-ref ref="EMAIL"/>
</root>
</configuration>
When the values are hardcoded in the logback.xml the email sending works as expected, however; after moving these to the JAVA_TOOL_OPTIONS bucket, the email stop working. The Lambda environment variables are correctly loaded on Lambda startup and can be accessed through the System.getProperty() in the Lambda context, providing the values from the environment variable.

Related

Can't apply xml-configuration for logback from config server

I'm developing an application with springboot which has an microservice architecture.
There is Service A (Port 8080) and spring cloud's config-server (Port 8888), which provides the properties-file for Service A from a Git-Repo (located locally under the ~/git//config).
This works well so far and Service A receive the properties-file from config-server.
For logging i use the logback logging framework and created a XML-Configuration file named logback-spring.xml with following content:
<?xml version="1.0" encoding="utf-8"?>
<property name="LOGS" value="./logs" />
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%highlight(%-5level) %black(%d{ISO8601}) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/spring-boot-logger.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>
<logger name="com.example" level="trace" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
I want to distribute this configuration file to Service A by the config-server to have a central point to manage this for all future services.
Inside of the git-repo for config i created a directory named logging with that file inside. So the local path to that file is: ~/git//config/logging/logback-spring.xml.
Related to the docs(documentation of config server) in my case i can retrieve this file with curl localhost:8888/config/spring/main/logging/logback.xml and when i curl this i get the content from this file.
But now the problem:
I specified this logging.config - location to this endpoint to let springboot load this XML file as a config client. The application-properties in Service A has following content:
When i start Service A while hoping to get this configuration appended to the logs, i am getting following error:
How can i properly include the configuration to Service A?
Appreciate any help.

Custom log file for specific packages in Spring boot

I have a java package with specialized operations. Specialized in the sense that they are rarely used and i don't want to have them be mixed with normal logging.
I know that adding logging.file=myapplication.log will redirect the logging to this file but is there a way to specify only logging from specific packages to another file? Like logging.file.my.package=special.log ?
It's not possible with the logging configuration Spring Boot provides. However, you can fall back upon the configuration that the logging framework provides. By default, this is Logback, which can be configured to log to multiple logging files.
To do that, you need to add a logback.xml file to your classpath and configure multiple appenders. For example:
<appender name="FILE1" class="ch.qos.logback.core.FileAppender">
<file>log1.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE2" class="ch.qos.logback.core.FileAppender">
<file>log2.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
And now you can define a separate logger in case you want to log to a different file. Make sure to add the additivity="false" otherwise, the log message will still be printed in both log files:
<logger name="com.example.apps.special-package" level="INFO" additivity="false">
<appender-ref ref="FILE2" />
</logger>
<root level="INFO">
<appender-ref ref="FILE1" />
</root>
In this case, all logs will be written to log1.log (FILE1 appender), while logs from the package com.example.apps.special-package will be written to log2.log (FILE2 appender).
Spring uses Logback as default logger. According to the official doc you can setup logback.xml yourself to add to the default logging mechanism your 'special' behavior:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<property name="SPECIAL_FILE_NAME" value="special"/>
<appender name="SPECIAL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} [%-10.10thread] %-5level %30.30logger{29}:%-4line %msg%n</pattern>
<charset>utf8</charset>
</encoder>
<file>logs/${SPECIAL_FILE_NAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>logs/${SPECIAL_FILE_NAME}-%i.log</fileNamePattern>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="logging.file.my.package" level="debug" additivity="false">
<appender-ref ref="SPECIAL_FILE"/>
</logger>
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="CONSOLE" />
</root>
</configuration>
Here we use Spring default FILE and CONSOLE appenders to log app info as usual (except logging.file.my.package), and use SPECIAL_FILE appender to log info from this package to file log/special.log.

How to pass properties from application.properties to logback config file

Overview:
I am using Sentry appender in my logback.xml file and I want to pass plenty of tags as parameters from application.properties file to logback config file.
logback.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<appender name="SENTRY" class="com.getsentry.raven.logback.SentryAppender">
<dsn>
https://e0a61232c92f42ffa34c22914d676a8e:e64f7edc60de490eb004556d2b3fce45#sentry.io/112817
</dsn>
<springProfile name="dev">
<tags>env:dev,app:${app.name},platform:aws</tags>
</springProfile>
<springProfile name="stage">
<tags>env:dev</tags>
</springProfile>
<springProfile name="test">
<tags>env:test</tags>
</springProfile>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
<root level="ERROR">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="SENTRY"/>
</root>
</configuration>
application.properties:
security.ignored=/**
logging.level.root = DEBUG
spring.profiles.active=dev
app.name=retailServices
Note:
the spring.profiles.active property in application.properties is mapped to springProfile tag in logback config file.
But the issue is the fact that the "app.name" property cannot be found in logback.xml file. If I use this property as system properties it works but I want to pass it to config file from application.properties.
So any solution, feedback and idea would be highly appreciated.
In logback.xml include:
<property resource="application.properties" />
And then you can refer properties in a standard way, for example ${app.name}.
I found that Spring has support of next tag <springProperty/> described here . It means that you can easily add variable from property files even this variable value spring resolves from environment/system variable.
you can do it now directly via a logback context, or via a third party encoder.
LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
context.putProperty("global", "value");
https://github.com/getsentry/sentry-java/pull/794
I know this is an old question but this is still a valid question.
Here is my solution, let's say you have a different spring profile and base on it want to change the configuration like log pattern, path of log file create, size of log file ...etc.
It can do it in like below,
let's say, We have two profile called dev and prod those config files look like below,
application-dev.properties
spring.application.name=Admin Module
logback.log.pattern=%-5level [%thread] %logger{36} : %X{correlationId} : %m%n
logback.app.log.root=C:\logs\adminModule\
logback.max.file.size=50MB
logback.max.file.history=2
application-prod.properties
spring.application.name=Admin Module
logback.log.pattern=%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %-5level [%thread] %logger{36} : ${spring.application.name} : %X{correlationId} : %m%n
logback.app.log.root=\logs\adminModule\
logback.max.file.size=50MB
logback.max.file.history=30
application.properties
# (active profile can get dynamically)
spring.profiles.active=dev
This is my logback.xml file
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<!-- get active profile-->
<springProperty scope="context" name="profile" source="spring.profiles.active"/>
<!-- get properties from active profile-->
<property resource="application-${profile}.properties" />
<property name="LOG_PATTERN" value="${logback.log.pattern}"/>
<property name="APP_LOG_ROOT" value="${logback.app.log.root}"/>
<property name="MAX_FILE_SIZE" value="${logback.max.file.size}"/>
<property name="MAX_FILE_HISTORY" value="${logback.max.file.history}"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="infoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APP_LOG_ROOT}/info.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${APP_LOG_ROOT}/info.%d{yyyy-MM-dd_HH}-%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>${MAX_FILE_HISTORY}</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="infoLog"/>
<appender-ref ref="console"/>
</root>
</configuration>
This solution will reduce multiple logback.xml files maintain over head.

MongoDbUtils debug logging

How can I disable the following logging from org.springframework.data.mongodb.core.MongoDbUtils:
DEBUG o.s.data.mongodb.core.MongoDbUtils - Getting Mongo Database name=[erepprod]
I have tried the following in my log4j.properties file:
log4j.category.org.springframework.data.mongodb.core.MongoDbUtils=ERROR,console
log4j.logger.org.springframework.data.mongodb.core.MongoDbUtils=ERROR,console
But the message is still being logged.
Any help would be appreciated as I have spent two days trying to get rid of this message.
Probably a bit late, but for anyone else having the problem,
I know from using Spring Boot that they seem to prefer now SL4J + Logback
So by adding a logback.xml that looked like below.. solved it:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p | %t | %-55logger{55} | %m %n</pattern>
</encoder>
</appender>
<logger name="org.springframework.data.mongodb">
<level value="INFO" />
</logger>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
</root>
</configuration>

hibernate logback sql

I want to see the actual parameters of my SQL queries when I use Hibernate. I add this to my logback.xml to see the queries (with question marks):
<logger name="org.hibernate.type" level="TRACE" />
but to no effect.
Is there any special configuration necessary?
OnConsoleStatusListener shows me the correct configuration
23:48:15,246 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.type] to TRACE
but no output from org.hibernate.type package.
I'm using Spring with Jpa.
Things you have to make sure:
Are you sure that SLF4J + LogBack is working in your app?
Is your logger pointing to any appender?
Have you configured an appended?
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<!-- "application-name" is a variable -->
<File>c:/logs/${application-name}.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d %p %t %c - %m%n</Pattern>
</layout>
</appender>
<root level="debug">
<appender-ref ref="FILE"/>
</root>
</configuration>
I'm using this configuration, and it works for me:
<logger name="org.hibernate.type" level="trace" additivity="false">
<appender-ref ref="consoleAppender" />
</logger>
The logger that works for me is the following:
<logger name="org.hibernate.type" level="TRACE" />

Resources