Warning when starting spring tool suite - spring

I am just starting out on trying the spring tool suite v3.4.0 under oracle java 7u40 on Ubuntu Desktop AMD64 13.10 However, when I start the application from the command prompt, I get the following warning:
log4j:WARN No appenders could be found for logger (org.springsource.ide.eclipse.commons.core.templates.TemplateProcessor).
log4j:WARN Please initialize the log4j system properly.
I probably need to configure log4j.properties somewhere, however I am not sure where and how I link the config to STS. Has anyone got a log4j.properties for STS and how do I link it to the STS startup.
I tried the following log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
And added the following environment variable:
export JAVA_OPTS=-Dlog4j.configurationFile=~/Log4j/log4j2.xml
However, I still get the same warning when starting STS.

You have a number of options:
Specify the location of the log4j configuration file on the file system. The path in a file: URL must be absolute. On Windows, file: URLs start with either "file:/", or "file:///", and '/' characters replace '\' characters in the path. Your line would be:
export JAVA_OPTS=-Dlog4j.configurationFile=file:/path/to/your/log4j2.xml
Specify the location of the log4j configuration file relative to a classpath entry. If the "/path/to/your/" directory is on the classpath your line would be:
export JAVA_OPTS=-Dlog4j.configurationFile=log4j2.xml
Specify the location of log4J configuration file to be used for all JVMs. Your line would be:
export _JAVA_OPTIONS=-Dlog4j.configurationFile=file:/path/to/your/log4j2.xml
Specify the location of the log4j configuration file on the Java command line for your application. Your line would be:
-Dlog4j.configurationFile=file:/path/to/your/log4j2.xml
In your question, you don't specify what application you are running, but these options will all work.
If you are trying to specify the log4j configuration to use in an application running on a JEE container, e.g. a web application, please refer to the log4j documentation.
I hope this helps you out.

My original assumption and answer were incorrect.
Setting the JVM option -Dlog4j.debug=true revealed that Eclipse ignores the log4j.configurationFile property and looks for either log4j.xml or logj.properties on the classpath.
I do not know how to change the system classpath for Eclipse, so I put the log4j configuration in the JRE's endorsed directory. The steps are:
Put your log4j configuration file in a jar file, say log4jconfig.jar
Put that jar file in the JRE's endorsed directory. On my Windows 7 system, this is "...\JDK1.7.0\jre\lib\endorsed". If the directory does not exist, create it.
The log4j configuration I used is:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="Console" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="ALL" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</layout>
</appender>
<root>
<level value="all" />
<appender-ref ref="Console" />
</root>
</log4j:configuration>
I hope this helps.

Related

Java Springboot Gradle project how to write log to cloudwatch?

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.

How to change log level in a spring boot project(in production enviroment) by change any property in application properties if I am using log4j2

I am developing a spring boot service (2.1.7 spring boot version) where I am using log4j2 to establish the logs and their pattern:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" name="MyService">
<Properties>
<Property name="project.component">${bundle:bootstrap:project.component}</Property>
<Property name="project.version">${bundle:bootstrap:project.version}</Property>
</Properties>
<Appenders>
<Console name="main" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss,SSSZ} ${project.component} ${project.version} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="root" level="INFO">
<AppenderRef ref="main" level="INFO"/>
</Logger>
</Loggers>
</Configuration>
I am not using log4j2.properties.
My problem is that I am only able to change the log level by log4j2.xml, I have tried with several spring boot properties in my application.properties as logging.level.root or through actuator endpoints but they did not work. When I am in a production environment and I need to change the logs level I have not a way to do it.
Any suggestion?
I am able to change the log level using a similar configuration, which even let's you configure a pattern for the log:
# Set everything to be logged to the console
log4j.rootCategory=TRACE, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n
Per-class log levels can also be set, in example:
# Settings to quiet third party logs that are too verbose
log4j.logger.org.eclipse.jetty=WARN
log4j.logger.org.eclipse.jetty.util.component.AbstractLifeCycle=ERROR
log4j.logger.org.apache.spark.repl.SparkIMain$exprTyper=INFO
log4j.logger.org.apache.spark.repl.SparkILoop$SparkILoopInterpreter=INF
log4j.logger.org.apache.spark.sql.execution.streaming.FileStreamSource=TRACE

How to ignore logback RollingFileAppender FileNotFoundException

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>

SpringBoot logging configuration based on logging level

Is it possible to specify in SpringBoot's appliaction.properties different logging configuration for different logging levels?
For example to log info to file but debug only to console and specify different format for them.
I searched in SpringBoot docs but couldn't find the answer.
You can try using the log4j2 logger and specifiying withing its configurating file multiple logger elements. If you have the log4j2 dependency set up in spring, it should pick up the configuration in the file log4j2-spring.xml. Take a look at their intro docs here, but i believe something like this in the log4j2 config file would suffice:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n%throwable"/>
</Console>
<File name="FileAppender" fileName="debug.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="ConsoleAppender"/>
</Root>
<Root level="DEBUG">
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
</Configuration>
Add following config in your application.yml
logging:
level:
root: INFO
com:
somepackage: DEBUG
someotherpackage: TRACE
Here you have set default logging to INFO (root logger) and for package com.somepackage logging level is DEBUG, for com.someotherpackage logging level is TRACE.

Spring - Exceptions do not log to a file

I'm currently using SLF4J API for logging.
Whenever an exception is thrown during runtime, the full error stack trace does not log to file, it is only printed to the console. I'm using eclipse.
Here is my code for logback.xml (currently located in classes folder under WEB-INF)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<!-- Specify here the path of the folder you want to save your logs -->
<property name="LOGFILE_PATH" value="C:/Logs" />
<!-- All logging will be redirected/ printed to console. -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger{50} - %rEx %msg%n </Pattern>
</layout>
</appender>
<!-- Send log to file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGFILE_PATH}/spring-mybatis-log.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger - %rEx %msg%n</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGFILE_PATH}/spring-mybatis-log-%d{yyyy-MM-dd}-%i.txt
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>2MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Is there something missing/wrong with the above file??
Is it possible to log (to a file) all the text that will be printed to console?
How does spring(or the project itself) read the logback.xml file? What if I rename it and place it in another folder?
How to create one root containing all the levels (INFO, DEBUG, ERROR, WARN, etc..) ?
To answer your questions:
Nothing looks obviously wrong to me in the file you've posted, although I didn't try actually running it.
Just the way you did it is fine, with two appenders, one that goes to the file and the other to go to the console.
Logback by default looks in the classpath for the logback.xml file. Refer to the configuration page of the manual for the details. The way it gets there depends on your build system. When using Maven, I'd suggest putting it in src/main/resources. But if it ends up in WEB-INF/classes when deployed in your web app, that should work. If no matter what you put in your logback.xml file you are only getting console output (try adding a syntax error or renaming the file to test), that's what I'd look at first, to ensure that Logback is picking up the file right. It will default to showing everything just on the console if it can't find the file, though I think it shows a warning at the beginning that it's doing so. If it is picking up the file, you can try putting debug="true" in the <configuration> element, to see if there's an error that it's picking up that's causing it to not use the appender the way you're expecting.
Specifying the "DEBUG" level of logging, as you've done, will also get all higher levels as well.
If the issue is that logging from Spring isn't going where you want, while your application's logging is working fine, you may need to redirect Spring (which uses Apache Commons Logging) to use SLF4J instead. To do that, remove the commons-logging dependency and add the jcl-over-slf4j library. This will emulate the commons-logging calls and have them point to SLF4J instead. See the "Using SLF4J" section of this blog post for more details.
Your configuration looks OK, but you can try use your root level INFO instead DEBUG and if you have frameworks like spring, hibernate etc. Logback allow you uses another levels to them something like:
<logger name="org.hibernate" level="OFF" />
<logger name="org.springframework" level="INFO" />
<logger name="org.springframework.web.servlet.mvc" level="INFO" />

Resources