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.
Related
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>
I have configured log4j2 in my gradle project to write the log to a local file. The log4j2.xml file is written as follow:
<Configuration>
<appender name="cloud-watch" class="io.github.dibog.AwsLogAppender">
<awsConfig>
<credentials>
<accessKeyId>XXXXX</accessKeyId>
<secretAccessKey>XXXXXX</secretAccessKey>
</credentials>
<region>us-east-2</region>
</awsConfig>
<createLogGroup>true</createLogGroup>
<queueLength>100</queueLength>
<groupName>SpringLog</groupName>
<streamName>StreamName</streamName>
<dateFormat>yyyyMMdd_HHmm</dateFormat>
<layout>
<pattern>[%thread] %-5level %logger{35} - %msg %n</pattern>
</layout>
</appender>
<Loggers>
<Root level="ALL">
<AppenderRef ref="cloud-watch"/>
</Root>
</Loggers>
</Configuration>
Here I am firstly getting an error "Unable to locate appender "cloud-watch". Also the logging to the cloudwatch is also not working.
Can any one please correct me what I am doing wrong. Your help is much appreciated. I tried consoling the log and it works fine.
Can anyone suggest on how to do logging from springboot gradle project to aws cloudwatch?
This is not a valid Log4j2 configuration. Log4j2 uses plugins and the class name of appenders, filters, layouts, etc is never specified in the configuration.
I found an example configuration similar to yours at https://github.com/dibog/cloudwatch-logback-appender. Note that the name of the project is cloudwatch-logback-appender, so this appender is designed to work with Logback, not Log4j2.
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>
When logging errors to stackdriver, every message is logged as INFO, even when using log.error or log.warn, etc., but the payload is correct.
I'd like to be able to filter by severity and get email on error.
I'm using Spring Boot and Logback. The app has been deployed on a Kubernetes Cluster on GCP.
Here is my logback-spring.xml
<configuration>
<include resource="org/springframework/cloud/gcp/autoconfigure/logging/logback-appender.xml" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss, UTC} %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<springProfile name="prod,qa">
<root level="WARN">
<appender-ref ref="STACKDRIVER" />
</root>
</springProfile>
</configuration>
And here is the dep added in Maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-logging</artifactId>
</dependency>
Spring Boot version: 2.1.3.RELEASE
Spring Cloud version: Greenwich.RELEASE
What is wrong with this config? Is there any other solution?
EDIT: Just realized that the STACKDRIVER appender above is not the one logging to Stackdriver, but STDOUT is enough (maybe bc it's a Kubernetes cluster?), but the issue persists
The Stackdriver logging agent configuration for Kubernetes defaults to INFO for any logs written to the container's stdout and ERROR for logs written to stderr. If you want finer-grained control over severity, you can configure Spring to log as single-line JSON (e.g., via JsonLayout1) and let the logging agent pick up the severity from the JSON object (see https://cloud.google.com/logging/docs/agent/configuration#process-payload).
1By default, JsonLayout will use "level" for the log level, while the Stackdriver logging agent recognizes "severity", so you may have to override addCustomDataToJsonMap.
See also GKE & Stackdriver: Java logback logging format?
Directly using google cloud logging logback appender takes the severity from the log level on each case:
In Maven:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-logging-logback</artifactId>
<version>0.116.0-alpha</version>
</dependency>
In logback.xml:
<appender name="Cloud" class="com.google.cloud.logging.logback.LoggingAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<log>YOUR_LOG_NAME</log>
<resourceType>container</resourceType>
<flushLevel>INFO</flushLevel>
</appender>
...
<logger name="org.springframework" level="WARN" additivity="true">
<appender-ref ref="Cloud"/>
</logger>
It is not the spring cloud component but solves the problem.
I have to put all the log data (i.e., debug, info, error) into mysql database instead of to file/console.
I read the spring boot documentation but I didn't see any configuration related to database for logging.
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
Also tried the following link but its also not working.
https://www.tutorialspoint.com/log4j/log4j_logging_database.htm
Can anyone help me to do this.
Thanks.
I read the spring boot documentation but I didn't see any
configuration related to database for logging.
Because spring boot hands off that functionality to logging framework (slf4j + logback/log4j etc). So you need to configure your logging framework accordingly using it's configuration file (eg: logback.xml, logback-spring.xml, logback.groovy etc). Default logging frameworks in Spring boot is slf4j+logback. So checkout how you can use DBAppender.
For Logback
https://logback.qos.ch/manual/appenders.html#DBAppender
http://learningviacode.blogspot.com/2014/01/writing-logs-to-database.html
Log to database with LogBack
https://medium.com/#chakrar27/storing-log-data-in-postgresql-using-logback-db-appender-292891a9918
1. create logback.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
</pattern>
</encoder>
</appender>
<appender name="db" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource
class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<driverClass>org.postgresql.Driver</driverClass>
<url>jdbc:postgresql://localhost:5432/simple</url>
<user>postgres</user>
<password>root</password> <!-- no password -->
</connectionSource>
</appender>
<!-- the level of the root level is set to DEBUG by default. -->
<root level="TRACE">
<appender-ref ref="stdout" />
<appender-ref ref="db" />
</root>
</configuration>
2. Create the 3 tables
logging_event
logging_event_property
logging_event_exception
They must exist before DBAppender can be used
For Log4J
https://logging.apache.org/log4j/2.x/manual/appenders.html#JDBCAppender
For Log4J2
http://smasue.github.io/log4j2-spring-database-appender