Logstash not reading spring properties correctly - java

So I have a quite simple logstash configuration that should read the host URL from spring. Although for some reason it is not working.
My logback-spring.xml looks sort of like this:
<springProperty name="logstashDestination" scope="context" source="service.logstash.host"/>
<appender class="net.logstash.logback.appender.LogstashTcpSocketAppender" name="LOGSTASH">
<destination>${logstashDestination}</destination>
<!-- encoder is required -->
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"service":"provisioning-engine-jane"}</customFields>
</encoder>
</appender>
In my application.properties I have these lines:
spring.profiles.active:sit
service.logstash.host:NaN
And in application-sit.properties I have this line:
service.logstash.host:api-test.local:9013
This makes the whole application crash with the error
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.util.PropertySetter#44cb3536 - Could not invoke method addDestination in class net.logstash.logback.appender.LogstashTcpSocketAppender with parameter of type java.lang.String java.lang.reflect.InvocationTargetException
ERROR in net.logstash.logback.appender.LogstashTcpSocketAppender[LOGSTASH] - No destination was configured. Use <destination> to add one or more destinations to the appender
The odd thing here is that if I add a bootstrap.properties file with some dummy data it starts. Issue with this is that the dummy data is overriding my environment specific logstash URL.
Anyone have any idea of why this might be happening?

Related

How to exclude io.undertow.request package from the logging in wildfly

I am getting following error logs related to io.undertow.request package.
Caused by: java.io.IOException: Broken pipe
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:51)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
at org.xnio.nio.NioSocketConduit.write(NioSocketConduit.java:153)
at io.undertow.protocols.ssl.SslConduit.doWrap(SslConduit.java:874)
at io.undertow.protocols.ssl.SslConduit.write(SslConduit.java:379)
at io.undertow.server.protocol.http.HttpResponseConduit.processWrite(HttpResponseConduit.java:247)
at io.undertow.server.protocol.http.HttpResponseConduit.write(HttpResponseConduit.java:588)
at io.undertow.conduits.AbstractFixedLengthStreamSinkConduit.write(AbstractFixedLengthStreamSinkConduit.java:106)
at io.undertow.conduits.AbstractFixedLengthStreamSinkConduit.write(AbstractFixedLengthStreamSinkConduit.java:120)
at io.undertow.conduits.DeflatingStreamSinkConduit.performFlushIfRequired(DeflatingStreamSinkConduit.java:418)
at io.undertow.conduits.DeflatingStreamSinkConduit.flush(DeflatingStreamSinkConduit.java:348)
at org.xnio.conduits.ConduitStreamSinkChannel.flush(ConduitStreamSinkChannel.java:162)
at io.undertow.channels.DetachableStreamSinkChannel.flush(DetachableStreamSinkChannel.java:119)
at org.xnio.channels.Channels.flushBlocking(Channels.java:63)
at io.undertow.servlet.spec.ServletOutputStreamImpl.close(ServletOutputStreamImpl.java:605)
In this case, it is not necessary to see those errors in my wildfly log. I need to exclude this package from the logging.Is there anyway that I can make a configuration for it?
If you dont want any logging from io.undertow.request then add below tag in standalone-*.xml file in logging subsystem:
<logger category="io.undertow.request">
<level name="OFF"/>
</logger>

Runnable JAR not detecting logback.xml configuration

I have a Java maven application in which I have created a swing GUI.
I am using logback-classic for logging purposes. logback.xml is present under the src/main/resources directory.
When I run the application using eclipse "Run As Java Application", it is working as intended and application is logging to the console and log file.
But after exporting the application as a runnable JAR file and double clicking to run the application, it is not logging to file.
When I opened command prompt and execute application using "java -jar myjarfile.jar", I am able to see the console logs, but it is using the default logback configuration.
I tried giving additional parameter, -Dlogback.configurationFile="path-to-file/logback.xml" and I am getting the below error,
Failed to instantiate [ch.qos.logback.classic.LoggerContext] Reported
exception: java.lang.IllegalArgumentException: name
at sun.misc.URLClassPath$Loader.findResource(Unknown Source)
at sun.misc.URLClassPath$1.next(Unknown Source)
at sun.misc.URLClassPath$1.hasMoreElements(Unknown Source)
at java.net.URLClassLoader$3$1.run(Unknown Source)
at java.net.URLClassLoader$3$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader$3.next(Unknown Source)
at java.net.URLClassLoader$3.hasMoreElements(Unknown Source)
at sun.misc.CompoundEnumeration.next(Unknown Source)
at sun.misc.CompoundEnumeration.hasMoreElements(Unknown Source)
at ch.qos.logback.core.util.Loader.getResources(Loader.java:73)
at ch.qos.logback.classic.util.ContextInitializer.multiplicityWarning(ContextInitializer.java:183)
at ch.qos.logback.classic.util.ContextInitializer.statusOnResourceSearch(ContextInitializer.java:175)
at ch.qos.logback.classic.util.ContextInitializer.findConfigFileURLFromSystemProperties(ContextInitializer.java:111)
at ch.qos.logback.classic.util.ContextInitializer.findURLOfDefaultConfigurationFile(ContextInitializer.java:120)
at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:148)
at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)
at org.slf4j.impl.StaticLoggerBinder.(StaticLoggerBinder.java:55)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
at com.esignature.tools.templatemigration.ConverterGUI.(ConverterGUI.java:35)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:59)
I don't know what I am missing and I want to log from runnable JAR to the log file.
It is confusing because it is working from eclipse but not from the exported runnable JAR.
Below is the logback.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_HOME" value="C:/template converter logs" />
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</pattern>
</encoder>
</appender>
<appender name="rollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/applog-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="rollingFile" />
</root>
</configuration>
The error you are facing is because there is two logback.xml configuration, one is in your classpath and you are trying to override/add another one using -Dlogback.configurationFile
In my case when packaging the runnable JAR I changed the pom configuration to this.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
Note the executable set to true, this makes sure that a MANIFEST.MF file is added to the JAR package.
So in order to make this work with this config I added this property to my application.yml/application.properties
logging:
config: file:D:\\LogConfig\\logback.xml
Then I proceed to generate the JAR file using clean, install mvn commands.
From there running the JAR file using java -jar myJar.jar works fine and there is no need to add arguments to search the application.yml or logback.xml. That is because the properties is being loaded from within the jar and the log config from the specified route on the application.yml .
Using maven shade plugin to generate a shaded JAR file that includes the dependencies and the custom logback.xml is one working solution that I utilized.
Now when I run this JAR file (either from command line or by double-clicking it), logs are generated in the specified location as expected.

Unable to create class org.apache.logging.log4j.core.impl.Log4jContextFactory

I am using spring boot application for a project and getting the following error while running the project from jar using java -jar build/libs/com.user.admin.rest.services.jar. But its not a blocker and i was able to access the api's, and the logger is working fine as intended. I just want to resolve this error.
ERROR StatusLogger Unable to create class org.apache.logging.log4j.core.impl.Log4jContextFactory specified in jar:file:/Users/Samuel/UserMgmt/com.user.admin.rest.services/build/libs/com.user.admin.rest.services.jar!/META-INF/log4j-provider.properties
java.lang.ClassNotFoundException: org.apache.logging.log4j.core.impl.Log4jContextFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.logging.log4j.spi.Provider.loadLoggerContextFactory(Provider.java:96)
at org.apache.logging.log4j.LogManager.<clinit>(LogManager.java:91)
at com.user.admin.rest.services.api.UserServiceResource.<clinit>(UserServiceResource.java:32)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at com.user.admin.rest.services.api.Application.main(Application.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
I have added the following lines for logger in the build.gradle file.
compile 'org.apache.logging.log4j:log4j-api:2.8.2'
compile 'org.apache.logging.log4j:log4j-core:2.8.2'
And my log4j2.xml file is given below
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="ERROR" name="example-config">
<Properties>
<Property name="PATTERN">%-5level %d [%t] %c:%M(%L): %m%n</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="${PATTERN}" />
</Console>
</Appenders>
<Loggers>
<Root level="TRACE">
<AppenderRef ref="STDOUT" />
</Root>
</Loggers>
</Configuration>
Generally speaking, you should never need to switch logging implementations; Logback should suit you fine. However, if you decide that you’d rather use Log4j or Log4j2, you’ll need to change your dependencies to include the appropriate starter for the logging implementation you want to use and to exclude Logback.
Spring Boot in Action
Swapping out Logback for Log4j2 in Gradle
Exclude the default logging
configurations {
all*.exclude group:'org.springframework.boot',
module:'spring-boot-starter-logging'
}
Add Log4j2 logging
compile("org.springframework.boot:spring-boot-starter-log4j2")
You can find details of spring-boot-starter-log4j2 dependency version that is provided by Spring Boot here.
I have met the similar problem. The version of log4j-api and log4j-core have to match (and ClassNotFound is a strange exception here). As you have said in comment, you have found log4j-core-2.7.jar, but I may guess your log4j-api is in version of 2.8.2 as your above description shows.
Check your dependency, remove old version of log4j-core and add new version should fix the problem.
This exception is printed to System.out because the class is not on the classpath of Spring's boot loader. But this is not the cause of logging not working or not using the specified configuration file.
It's a Spring Boot Gradle plugin issue. It should add Log4j to the boot loader classpath when it detects a dependency on Log4j.

Running Spring Integration within Windows Service; Does Not "Trigger" Inbound

I've got a Spring Boot command line application using Spring Integration. The application is running within a service wrapper.
At start up, everything seems to run smoothly with no exceptions. However, if I drop a file in the observed directory nothing triggers the inbound-channel-adapter.
Is it at all possible the service wrapper is causing the polling to not run?
UPDATE
I added a logback.xml configuration
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework.integration" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
</configuration>
But now the log is not receiving any input with this config. STDOUT works fine without the config. Makes is difficult to enable DEBUG for the integration package.
I based my wrapper on this project
UPDATE #2
I finally got logging working. I had a mistake in assuming it was not a ERROR level issue.
13:29:52.925 [task-scheduler-10] ERROR o.s.i.handler.LoggingHandler - org.springframework.messaging.MessagingException: The path [\\server\sharepath] does not denote a properly accessible directory.
at org.springframework.integration.file.DefaultDirectoryScanner.listFiles(DefaultDirectoryScanner.java:83)
at org.springframework.integration.file.FileReadingMessageSource.scanInputDirectory(FileReadingMessageSource.java:293)
at org.springframework.integration.file.FileReadingMessageSource.receive(FileReadingMessageSource.java:272)
at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:175)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:224)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:57)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:176)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:173)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:330)
at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:55)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:51)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:324)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
The issue is that the network path cannot be determined if accessible by Java/Spring.
13:29:52.925 [task-scheduler-10] ERROR o.s.i.handler.LoggingHandler -
org.springframework.messaging.MessagingException: The path
[\server\sharepath] does not denote a properly accessible directory.

Log4j2 SyslogAppender not working

I'm using Log4j version 2.1 (the latest stable version) and trying to use a Syslog appender to log to a syslog server. I'm using the configuration given at their official site (http://logging.apache.org/log4j/2.x/manual/appenders.html#SyslogAppender):
<Syslog name="RFC5424" format="RFC5424" host="localhost" port="514"
protocol="UDP" appName="MyApp" includeMDC="true"
facility="LOCAL0" enterpriseNumber="18060" newLine="true"
messageId="Audit" id="App"/>
When I start my app and do a LogManager.getLogger(), I get an error (stack trace bellow), so, before I report an error to their bug tracker, am I doing something wrong?
014-11-17 18:26:23,640 ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.SyslogAppender for element Syslog. java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:766)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:706)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:698)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:358)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:161)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:359)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:420)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:138)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:147)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:175)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:426)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:408)
at Log.<clinit>(Log.java:7)
Caused by: java.lang.IllegalArgumentException: No structured id name was supplied
at org.apache.logging.log4j.message.StructuredDataId.<init>(StructuredDataId.java:92)
at org.apache.logging.log4j.core.layout.Rfc5424Layout.<init>(Rfc5424Layout.java:139)
at org.apache.logging.log4j.core.layout.Rfc5424Layout.createLayout(Rfc5424Layout.java:657)
at org.apache.logging.log4j.core.appender.SyslogAppender.createAppender(SyslogAppender.java:133)
... 19 more
You need to include the mdcId attribute. e.g.:
<Syslog name="RFC5424" format="RFC5424" host="localhost" port="514"
protocol="UDP" appName="MyApp" mdcId="mdc" includeMDC="true"
facility="LOCAL0" enterpriseNumber="18060" newLine="true"
messageId="Audit" id="App"/>

Categories