Java cannot use logback inside JAR - java

I am trying to use logback while running my ap outside of project ( inside the JAR file ). I have tried to move it in the root folder and run it with the following command:
java -Dlogback.configurationFile=conf/logback.xml -jar xxxxx.jar
but it didn't work.When I am running the project from intelliJ everything work as expecte but when I build the artifacts it doesn't work anymore.
I am using JavaFX with dependencies, I don't use maven.
My Logback xml file is this:
<configuration>
<timestamp key="TIMESTAMP" datepattern="HHmmss"/>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${user.home}/vlcj-${TIMESTAMP}.log</file>
<append>false</append>
<encoder>
<pattern>%-36(%d{HH:mm:ss.SSS} [%thread]) %-5level %72logger{72} - %msg%n</pattern>
</encoder>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-36(%d{HH:mm:ss.SSS} [%thread]) %-5level %72logger{72} - %msg%n</pattern>
</encoder>
</appender>
<logger name="ch" level="ERROR"/>
<logger name="org" level="ERROR"/>
<logger name="com" level="ERROR"/>
<logger name="uk.co.caprica.vlcj" level="DEBUG"/>
<root level="debug">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
I get the following error:
10:43:01,092 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT
find resource [logback.groovy]
10:43:01,092 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found res
ource [logback-test.xml] at [jar:file:/D:/DigiStorageStreamer/out/artifacts/Digi
StorageStreamer/DigiStorageStreamer.jar!/logback-test.xml]
10:43:01,099 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList#6af6
b7d7 - URL [jar:file:/D:/DigiStorageStreamer/out/artifacts/DigiStorageStreamer/D
igiStorageStreamer.jar!/logback-test.xml] is not of type file
10:43:01,126 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction -
debug attribute not set
10:43:01,127 |-ERROR in ch.qos.logback.core.joran.action.TimestampAction - Attri
bute named [datePattern] cannot be empty
10:43:01,127 |-INFO in ch.qos.logback.core.joran.action.TimestampAction - Using
current interpretation time, i.e. now, as time reference.
10:43:01,127 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About t
o instantiate appender of type [ch.qos.logback.core.FileAppender]
10:43:01,131 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming
appender as [FILE]
10:43:01,144 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA
- Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] fo
r [encoder] property
10:43:01,161 |-INFO in ch.qos.logback.core.FileAppender[FILE] - File property is
set to [C:\Users\Marian Pavel/vlcj-TIMESTAMP_IS_UNDEFINED.log]
10:43:01,163 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About t
o instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
10:43:01,163 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming
appender as [CONSOLE]
10:43:01,164 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA
- Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] fo
r [encoder] property
10:43:01,165 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Settin
g level of logger [ch] to ERROR
10:43:01,165 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Settin
g level of logger [org] to ERROR
10:43:01,165 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Settin
g level of logger [com] to ERROR
10:43:01,165 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Settin
g level of logger [uk.co.caprica.vlcj] to DEBUG
10:43:01,165 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Se
tting level of ROOT logger to DEBUG
10:43:01,165 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Atta
ching appender named [CONSOLE] to Logger[ROOT]
10:43:01,166 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Atta
ching appender named [FILE] to Logger[ROOT]
10:43:01,166 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction -
End of configuration.
10:43:01,166 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator#2ffd1032 -
Registering current configuration as safe fallback point
10:43:01.221 [JavaFX Application Thread] INFO
uk.co.caprica.vlcj.Info - vlcj: 3.9.0
10:43:01.223 [JavaFX Application Thread] INFO
uk.co.caprica.vlcj.Info - java: 1.8.0_66 Oracle Corporation
10:43:01.223 [JavaFX Application Thread] INFO
uk.co.caprica.vlcj.Info - java home: C:\Program Files\Java\jre1.
8.0_66
10:43:01.224 [JavaFX Application Thread] INFO
uk.co.caprica.vlcj.Info - os: Windows 10 10.0 amd64
Exception in thread "JavaFX Application Thread" java.lang.UnsatisfiedLinkError:
Unable to load library 'libvlc': JNA native support (win32-amd64/libvlc.dll) not
found in resource path (DigiStorageStreamer.jar)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:220)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:322)
at com.sun.jna.Library$Handler.<init>(Library.java:142)
at com.sun.jna.Native.loadLibrary(Native.java:387)
at com.sun.jna.Native.loadLibrary(Native.java:366)
at uk.co.caprica.vlcj.binding.LibVlc.<clinit>(LibVlc.java:115)
at Player.<init>(Player.java:72)
at Main$1.handle(Main.java:86)
at Main$1.handle(Main.java:63)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unkn
own Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknow
n Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknow
n Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(U
nknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$KeyHandler.process(Unknown Source)
at javafx.scene.Scene$KeyHandler.access$1800(Unknown Source)
at javafx.scene.Scene.impl_processKeyEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.
run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.
run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent
$353(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown
Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(Unknow
n Source)
at com.sun.glass.ui.View.handleKeyEvent(Unknown Source)
at com.sun.glass.ui.View.notifyKey(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Change your " <timestamp key="TIMESTAMP" datepattern="HHmmss"/>" in the xml to "datePattern" with uppercase "P"

Could you try full path logback.xml and place logback.xml outside Jar?

Related

Where to save logback.xml file for use of Logger slf4j in Java

I have a question regarding the location where I should save my logback.xml file so that my program can find it. I now saved it in src/main/resources, but it cannot find it. I know this since
String URL = "logback.xml";
System.out.println(ClassLoader.getSystemResource(URL));
returns null.
The logger does work, but prints all information to the command line
null
13:05:26.606 [main] DEBUG o.j.f.c.master.AbstractMaster - Number of columns: 18
13:05:26.613 [main] DEBUG o.j.f.c.master.AbstractMaster - Objective value: 479.0
13:05:26.666 [main] DEBUG o.j.f.c.master.AbstractMaster - Number of columns: 113
13:05:26.668 [main] DEBUG o.j.f.c.master.AbstractMaster - Objective value: 272.0
13:05:26.693 [main] DEBUG o.j.f.c.master.AbstractMaster - Number of columns: 118
13:05:26.694 [main] DEBUG o.j.f.c.master.AbstractMaster - Objective value: 238.0
13:05:26.721 [main] INFO o.j.f.c.master.AbstractMaster - Best objective value found: 238.0
13:05:26.721 [main] INFO o.j.f.c.master.AbstractMaster - (d1-s2-s2-d1) (s2-c1-s2) (s2-c3-c2-
s2);238.0 chosen with value: 1.0 resulting in cost: 238.0
The content of my logback.xml file is
<configuration scan="true" info="true">
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>myApp.log</file>
<append>false</append>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%msg%n</pattern>
</layout>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<logger name="algorithms.MultiLabel.cg.master" level="OFF"/>
<root level="debug"> <!-- levels: OFF, info, debug -->
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
Next I make some changes to the logback.xml file and again save it in the src/main/resources folder. If I now run my program again, I suddenly get the following output.
file:./target/classes/logback.xml
13:06:41,631 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:06:41,631 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:06:41,632 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:./target/classes/logback.xml]
13:06:41,715 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
13:06:41,720 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[.\target\classes\logback.xml]] every 60 seconds.
13:06:41,720 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
13:06:41,731 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.FileAppender]
13:06:41,733 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE]
13:06:41,783 |-WARN in ch.qos.logback.core.FileAppender[FILE] - This appender no longer admits a layout as a sub-component, set an encoder instead.
13:06:41,783 |-WARN in ch.qos.logback.core.FileAppender[FILE] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
13:06:41,783 |-WARN in ch.qos.logback.core.FileAppender[FILE] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
13:06:41,783 |-INFO in ch.qos.logback.core.FileAppender[FILE] - File property is set to [myApp.log]
13:06:41,787 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
13:06:41,789 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
13:06:41,790 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:06:41,797 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [algorithms] to OFF
13:06:41,797 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
13:06:41,797 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[ROOT]
13:06:41,798 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
13:06:41,798 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:06:41,800 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator#36f6e879 - Registering current configuration as safe fallback point
Best objective value found: 238.0
(d1-s2-s2-d1) (s2-c1-s2) (s2-c3-c2-s2);238.0 chosen with value: 1.0 resulting in cost: 238.0
Why does it now find the file in the folder ./target (where the src folder also is located), even though I did not save it there?
All information that it is printing is coming from the master package, which I set to "OFF", so how come it is still printing it?
I am looking forward to any suggestions/explanations!

Creating different MarkerFilters according to a certain condition in Logback.xml

Recently I've been working with logback and everything was going smooth until I tried to use Marker Filters together with the conditional xml tags.
What I wanted to do is to have a marker filtering system that would, depending on a property previously set, choose the adequate marker filters for my appenders.
My current code is bellow:
<configuration scan="true" scanPeriod="30 seconds">
<if condition='property("severityLevel").equals("SEVERITY-2")'>
<then>
<turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
<Marker>SEVERITY-1</Marker>
<OnMatch>DENY</OnMatch>
</turboFilter>
</then>
</if>
<!-- Console Appender -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - ${appName} - %marker - %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
</root>
</configuration>
The property severityLevel comes from the configurator property that I set before initializing the logger:
LoggerContext aLoggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator aConfigurator = new JoranConfigurator();
aConfigurator.setContext(aLoggerContext);
aLoggerContext.reset();
aLoggerContext.putProperty(APPLICATION_NAME, config.getApplicationName());
aLoggerContext.putProperty(LOG_MAX_FILE_SIZE, config.getLogMaxFileSize().toString());
aLoggerContext.putProperty(LOG_KEEP_FOR_DAYS, config.getLogKeepForDays().toString());
aLoggerContext.putProperty("severityLevel", "SEVERITY-2");
System.out.println(aLoggerContext.getProperty("severityLevel"));
try
{
aConfigurator.doConfigure(FileUtils.cleanPath(FileUtils.determineServletRealPath(""), false) + LOGBACK_CONFIGURATION_FILE_PATH);
}
catch (JoranException e)
{
StatusPrinter.printIfErrorsOccured(aLoggerContext);
throw e;
}
The expected behaviour would be that only SEVERITY-2 level messages would be logged. Instead, I keep getting all messages.
If I take the If condition out of the logback.xml file it will work fine but I still need to have another condition for a SEVERITY-3 in which both level 2 and level 1 will be denied and therefore I would need the If condition.
Some status messages from the logs:
|-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
|-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
|-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [vfs:/Applications/wildfly-10.1.0.Final/standalone/deployments/mw-file-extractor.war/WEB-INF/classes/logback.xml]
|-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList#6b14b260 - URL [vfs:/Applications/wildfly-10.1.0.Final/standalone/deployments/mw-file-extractor.war/WEB-INF/classes/logback.xml] is not of type file
|-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
|-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [vfs:/Applications/wildfly-10.1.0.Final/standalone/deployments/mw-file-extractor.war/WEB-INF/classes/logback.xml]
|-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 30 seconds
|-ERROR in ch.qos.logback.core.joran.conditional.IfAction - Could not find Janino library on the class path. Skipping conditional processing.
|-ERROR in ch.qos.logback.core.joran.conditional.IfAction - See also http://logback.qos.ch/codes.html#ifJanino
|-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
|-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
|-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
|-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
|-INFO in ch.qos.logback.classic.joran.JoranConfigurator#726fa157 - Registering current configuration as safe fallback point
Does anyone know what am I doing wrong?
Found out, with the help of #Ceki and Logback debug messages that I was missing the Janino library for the use of conditionals.
Adding both janino-3.0.8.jar and commons-compiler-3.0.8.jar fixed the issue.

Logback and SMTPAppender Issue

I added Logback to my Spring Boot project and it works when writing in files or standard output. Now I would like to send log emails, but if I try to configure the SMTPAppender, I get the following error:
Logging system failed to initialize using configuration from
'logback-spring.xml' java.lang.IllegalStateException: Logback
configuration error detected: ERROR in
ch.qos.logback.core.joran.action.AppenderAction - Could not create an
Appender of type [ch.qos.logback.classic.net.SMTPAppender].
ch.qos.logback.core.util.DynamicClassLoadingException: Failed to
instantiate type ch.qos.logback.classic.net.SMTPAppender ERROR in
ch.qos.logback.core.joran.spi.Interpreter#8:76 - ActionException in
Action for tag [appender]
ch.qos.logback.core.joran.spi.ActionException:
ch.qos.logback.core.util.DynamicClassLoadingException: Failed to
instantiate type ch.qos.logback.classic.net.SMTPAppender ERROR in
ch.qos.logback.core.joran.action.AppenderRefAction - Could not find an
appender named [EMAIL]. Did you define it below instead of above in
the configuration file? ERROR in
ch.qos.logback.core.joran.action.AppenderRefAction - See
http://logback.qos.ch/codes.html#appender_order for more details. at
org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:162)
at
org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:66)
at
org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:56)
at
org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:115)
at
org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:308)
at
org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276)
at
org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
at
org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:212)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
at
org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:73)
at
org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
at
org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:336)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at
org.lh.cfpappserver.CfpAppServerApplication.main(CfpAppServerApplication.java:10)
Exception in thread "main" java.lang.IllegalStateException:
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.action.AppenderAction - Could not
create an Appender of type [ch.qos.logback.classic.net.SMTPAppender].
ch.qos.logback.core.util.DynamicClassLoadingException: Failed to
instantiate type ch.qos.logback.classic.net.SMTPAppender ERROR in
ch.qos.logback.core.joran.spi.Interpreter#8:76 - ActionException in
Action for tag [appender]
ch.qos.logback.core.joran.spi.ActionException:
ch.qos.logback.core.util.DynamicClassLoadingException: Failed to
instantiate type ch.qos.logback.classic.net.SMTPAppender ERROR in
ch.qos.logback.core.joran.action.AppenderRefAction - Could not find an
appender named [EMAIL]. Did you define it below instead of above in
the configuration file? ERROR in
ch.qos.logback.core.joran.action.AppenderRefAction - See
http://logback.qos.ch/codes.html#appender_order for more details. at
org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:315)
at
org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276)
at
org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
at
org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:212)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
at
org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:73)
at
org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
at
org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:336)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at
org.lh.cfpappserver.CfpAppServerApplication.main(CfpAppServerApplication.java:10)
Caused by: java.lang.IllegalStateException: Logback configuration
error detected: ERROR in
ch.qos.logback.core.joran.action.AppenderAction - Could not create an
Appender of type [ch.qos.logback.classic.net.SMTPAppender].
ch.qos.logback.core.util.DynamicClassLoadingException: Failed to
instantiate type ch.qos.logback.classic.net.SMTPAppender ERROR in
ch.qos.logback.core.joran.spi.Interpreter#8:76 - ActionException in
Action for tag [appender]
ch.qos.logback.core.joran.spi.ActionException:
ch.qos.logback.core.util.DynamicClassLoadingException: Failed to
instantiate type ch.qos.logback.classic.net.SMTPAppender ERROR in
ch.qos.logback.core.joran.action.AppenderRefAction - Could not find an
appender named [EMAIL]. Did you define it below instead of above in
the configuration file? ERROR in
ch.qos.logback.core.joran.action.AppenderRefAction - See
http://logback.qos.ch/codes.html#appender_order for more details. at
org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:162)
at
org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:66)
at
org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:56)
at
org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:115)
at
org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:308)
... 13 more
This is my configuration in logback-spring.xml file:
<?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"/>
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<asynchronousSending>false</asynchronousSending>
<smtpHost>smtp.gmail.com</smtpHost>
<to>my email</to>
<from>my email</from>
<username>xxx</username>
<password>xxx</password>
<subject>%logger{20} - %m</subject>
<layout class="ch.qos.logback.classic.html.HTMLLayout"/>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
<bufferSize>1</bufferSize>
</cyclicBufferTracker>
<starttls>true</starttls>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="org.lh.cfpappserver.util.StdOutFilter"/>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE"/>
<appender-ref ref="EMAIL"/>
<appender-ref ref="STDOUT"/>
</root>
</configuration>
What's wrong in my configuration preventing the application to start?

Logback: logger functions not print msg in console.

I am new with logging, and using logback for my project. I create the logger configuration file logback.xml at put at the class path in maven project this is at resource root folder. Following is my configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<contextName>It-Support</contextName>
<timestamp key="timeFormat" datePattern="yyyyMMdd'T'HHmmss" timeReference="contextBirth" />
<property resource="properties/logback-variables.properties" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${file.record}/log.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>6</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appernder-ref ref="STDOUT" />
<!-- <appernder-ref ref="ROLLING" /> -->
</root>
</configuration>
In my java code, i am using logger.info, logger,error etc methods for print the information on console, but when the code is run, logger not able to print the information at eclipse console. My java code as below:
private Logger logger = LoggerFactory.getLogger(MachineController.class);
public String machinesList(Model model) {
logger.info("call machinesList controller GET");
---------------------
}
When i start the tomcat, the server print below logs:
SEVERE: The web application [/it-support] created a ThreadLocal with key of type [com.microsoft.sqlserver.jdbc.ActivityCorrelator$1] (value [com.microsoft.sqlserver.jdbc.ActivityCorrelator$1#31924c14]) and a value of type [com.microsoft.sqlserver.jdbc.ActivityId] (value [b1d32454-45c4-4684-9339-b12835b563d2-0]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
15:13:00,945 |-INFO in ch.qos.logback.classic.LoggerContext[It-Support] - Could NOT find resource [logback.groovy]
15:13:00,946 |-INFO in ch.qos.logback.classic.LoggerContext[It-Support] - Could NOT find resource [logback-test.xml]
15:13:00,946 |-INFO in ch.qos.logback.classic.LoggerContext[It-Support] - Found resource [logback.xml] at [jar:file:/D:/tommy8/webapps/it-support-web/WEB-INF/lib/it-support-common-0.0.1-SNAPSHOT.jar!/logback.xml]
15:13:01,004 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList#20b42477 - URL [jar:file:/D:/tommy8/webapps/it-support-web/WEB-INF/lib/it-support-common-0.0.1-SNAPSHOT.jar!/logback.xml] is not of type file
15:13:01,078 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
15:13:01,101 |-INFO in ch.qos.logback.classic.joran.action.ContextNameAction - Setting logger context name as [It-Support]
15:13:01,101 |-INFO in ch.qos.logback.core.joran.action.TimestampAction - Using context birth as time reference.
15:13:01,107 |-INFO in ch.qos.logback.core.joran.action.TimestampAction - Adding property to the context with key="timeFormat" and value="20150204T151300" to the LOCAL scope
15:13:01,111 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
15:13:01,119 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
15:13:01,165 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:13:01,285 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:13:01,291 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [ROLLING]
15:13:01,361 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
15:13:01,365 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern log-%d{yyyy-MM-dd}.%i.log for the active file
15:13:01,371 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP#28345639 - The date pattern is 'yyyy-MM-dd' from file name pattern 'log-%d{yyyy-MM-dd}.%i.log'.
15:13:01,371 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP#28345639 - Roll-over at midnight.
15:13:01,372 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP#28345639 - Setting initial period to Thu Jan 08 20:09:57 IST 2015
15:13:01,375 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:13:01,381 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ROLLING] - Active log file name: logs/log.log
15:13:01,381 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ROLLING] - File property is set to [logs/log.log]
15:13:01,384 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
15:13:01,394 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter#30:33 - no applicable action for [appernder-ref], current ElementPath is [[configuration][root][appernder-ref]]
15:13:01,394 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
15:13:01,397 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator#63f55760 - Registering current configuration as safe fallback point
It's "appender-ref", not "appernder-ref".
That is why your appender is not registered with the root-logger:
15:13:01,394 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter#30:33 - no applicable action for [appernder-ref], current ElementPath is [[configuration][root][appernder-ref]]

Logback SMTPAppender cannot find JNDI location and start

Using logback-1.0.13.jar and JDK 1.6u34.
I have a Java web app (WAR) with the following WEB-INF/classes/logback.xml:
<configuration debug="true" scan="true" scanPeriod="5 minutes">
<appender name="logManager-smtpAppender" class="ch.qos.logback.classic.net.SMTPAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>NEUTRAL</onMismatch>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<asynchronousSending>false</asynchronousSending>
<sessionViaJNDI>true</sessionViaJNDI>
<jndiLocation>java:comp/env/mail/Session-local</jndiLocation>
<subject>%logger{20} - %m</subject>
<layout class="ch.qos.logback.classic.html.HTMLLayout"/>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
<bufferSize>25</bufferSize>
</cyclicBufferTracker>
</appender>
<root level="ALL">
<appender-ref ref="logManager-smtpAppender" />
</root>
</configuration>
And the following ${TOMCAT_HOME}/conf/context.xml (global context.xml for all web apps):
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource
name="mail/Session-local"
auth="Container"
type="javax.mail.Session"
mail.smtp.host="my.smtp.server"
mail.user="smtp_user"
mail.password="smtp_password"
mail.transport.protocol="smtp"
mail.smtp.auth="true"
mail.smtp.port="25"
mail.smtp.starttls.enable="true"
/>
</Context>
And have the Java Mail API 1.4.3 (which I have verified contains a javax.mail.Authenticator class) on my runtime classpath at WEB-INF/lib/javax-mail-1.4.3.jar). I also have it in Tomcat's lib directory (${TOMCAT_HOME}/lib).
And am getting the following stack trace:
13:34:09,178 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:34:09,179 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:34:09,181 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/myuser/sandbox/dsi/workbench/tomcat/6.0.32/apache-tomcat-6.0.32/webapps/myapp/WEB-INF/classes/logback.xml]
13:34:09,469 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 5 minutes
13:34:09,470 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[/home/myuser/sandbox/dsi/workbench/tomcat/6.0.32/apache-tomcat-6.0.32/webapps/myapp/WEB-INF/classes/logback.xml]] every 300 seconds.
13:34:09,470 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
13:34:11,782 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.net.SMTPAppender]
13:34:11,830 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [logManager-smtpAppender]
13:34:11,939 |-INFO in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Looking up javax.mail.Session at JNDI location [java:comp/env/mail/Session-local]
13:34:11,992 |-ERROR in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Failed to obtain javax.mail.Session from JNDI location [java:comp/env/mail/Session-local]
13:34:11,992 |-ERROR in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Failed to obtain javax.mail.Session. Cannot start.
13:34:11,993 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to ALL
13:34:11,996 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [logManager-smtpAppender] to Logger[ROOT]
13:34:11,997 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:34:12,000 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator#63220fd1 - Registering current configuration as safe fallback point
13:39:49,988 |-WARN in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Attempted to append to non started appender [logManager-smtpAppender].
13:39:49,988 |-WARN in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Attempted to append to non started appender [logManager-smtpAppender].
13:39:50,598 |-WARN in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Attempted to append to non started appender [logManager-smtpAppender].
13:39:51,275 |-WARN in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Attempted to append to non started appender [logManager-smtpAppender].
13:39:51,833 |-WARN in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Attempted to append to non started appender [logManager-smtpAppender].
Any ideas as to why Logback can't retrieve my JNDI Mail Session from Tomcat's context.xml? This is preventing the SMTPAppender from starting and being able to log messages. Thanks in advance!
First, don't put the mail JAR in both locations, but in Tomcat's lib folder. If you are using Maven, use provided scope for the corresponding <dependency /> to not package it in the WAR.
Second, do you have a <resource-ref /> element for the mail session in your web.xml? If not, look here for a sample.
Another alternative is to make the session <resource /> global (in server.xml) and link to it from the context.xml, as described here.
HTH,
Jukka

Categories