我尝试搜索错误的解决方案,但遗憾的是找不到任何结果。
获得一个简单的" Hello World"在slf4j中使用log4j工作,我想扩展它并在SYSLOG中添加一个条目。此时我遇到了一个错误,我无法弄清楚。
我的项目基本上是maven hello world项目,稍作调整:
App.java:
package xx.xxxx.logging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
System.out.println("Hello World!");
LOGGER.info("Hello Logger!");
// Marker syslog = MarkerFactory.getMarker("SYSLOG");
// LOGGER.info(syslog, "Hello Syslog!");
}
}
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<Appenders>
<File name="FILE" fileName="${java.io.tmpdir}/consumer.log">
<PatternLayout pattern="%-5p %c{1}:%L - %m%n"/>
</File>
<Console name="STDOUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</Console>
<Syslog name="SYSLOG" format="RFC5424" host="localhost" port="514" protocol="UDP"
appName="UAI" mdcId="mdc" includeMDC="true" newLine="true">
<MarkerFilter marker="SYSLOG" onMatch="ACCEPT" onMismatch="DENY"/>
</Syslog>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="FILE"/>
<AppenderRef ref="SYSLOG"/>
</Root>
</Loggers>
</Configuration>
的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xx.xxxx.logging</groupId>
<artifactId>logging-test</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>logging-test</name>
<properties>
<jdk.version>1.8</jdk.version>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<sonar.java.source>${java.version}</sonar.java.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.sl4j>1.7.21</version.sl4j>
<version.sl4j.log4j>2.0.1</version.sl4j.log4j>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${version.sl4j}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${version.sl4j.log4j}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${version.sl4j.log4j}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${version.sl4j.log4j}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>xx.xxxx.logging.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
命令&mvn package&#39;说&#34;建立成功&#34;。
尝试运行App.java会产生以下错误:
root@blabla:~/test-syslog/logging-test# java -cp target/logging-test-1.0-SNAPSHOT.jar:target/libs/* xx.xxxx.logging.App
2017-12-08 10:45:28,083 ERROR Could not instantiate DatagramSocket to localhost java.net.SocketException: Invalid argument
at java.net.PlainDatagramSocketImpl.datagramSocketCreate(Native Method)
at java.net.AbstractPlainDatagramSocketImpl.create(AbstractPlainDatagramSocketImpl.java:80)
at java.net.DatagramSocket.createImpl(DatagramSocket.java:337)
at java.net.DatagramSocket.<init>(DatagramSocket.java:239)
at java.net.DatagramSocket.<init>(DatagramSocket.java:196)
at org.apache.logging.log4j.core.net.DatagramOutputStream.<init>(DatagramOutputStream.java:72)
at org.apache.logging.log4j.core.net.DatagramSocketManager$DatagramSocketManagerFactory.createManager(DatagramSocketManager.java:114)
at org.apache.logging.log4j.core.net.DatagramSocketManager$DatagramSocketManagerFactory.createManager(DatagramSocketManager.java:103)
at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:71)
at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:60)
at org.apache.logging.log4j.core.net.DatagramSocketManager.getSocketManager(DatagramSocketManager.java:65)
at org.apache.logging.log4j.core.appender.SocketAppender.createSocketManager(SocketAppender.java:168)
at org.apache.logging.log4j.core.appender.SyslogAppender.createAppender(SyslogAppender.java:140)
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.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:133)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:744)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:683)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:675)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:349)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:150)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:364)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:422)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:146)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:75)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:37)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:263)
at org.apache.logging.slf4j.Log4jLoggerFactory$PrivateManager.getContext(Log4jLoggerFactory.java:98)
at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:84)
at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:44)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
at xx.xxxx.logging.App.<clinit>(App.java:11)
2017-12-08 10:45:28,185 ERROR catching 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.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:133)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:744)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:683)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:675)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:349)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:150)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:364)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:422)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:146)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:75)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:37)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:263)
at org.apache.logging.slf4j.Log4jLoggerFactory$PrivateManager.getContext(Log4jLoggerFactory.java:98)
at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:84)
at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:44)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
at xx.xxxx.logging.App.<clinit>(App.java:11)
Caused by: org.apache.logging.log4j.core.appender.AppenderLoggingException: Could not instantiate DatagramSocket to localhost
at org.apache.logging.log4j.core.net.DatagramOutputStream.<init>(DatagramOutputStream.java:76)
at org.apache.logging.log4j.core.net.DatagramSocketManager$DatagramSocketManagerFactory.createManager(DatagramSocketManager.java:114)
at org.apache.logging.log4j.core.net.DatagramSocketManager$DatagramSocketManagerFactory.createManager(DatagramSocketManager.java:103)
at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:71)
at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:60)
at org.apache.logging.log4j.core.net.DatagramSocketManager.getSocketManager(DatagramSocketManager.java:65)
at org.apache.logging.log4j.core.appender.SocketAppender.createSocketManager(SocketAppender.java:168)
at org.apache.logging.log4j.core.appender.SyslogAppender.createAppender(SyslogAppender.java:140)
... 22 more
Caused by: java.net.SocketException: Invalid argument
at java.net.PlainDatagramSocketImpl.datagramSocketCreate(Native Method)
at java.net.AbstractPlainDatagramSocketImpl.create(AbstractPlainDatagramSocketImpl.java:80)
at java.net.DatagramSocket.createImpl(DatagramSocket.java:337)
at java.net.DatagramSocket.<init>(DatagramSocket.java:239)
at java.net.DatagramSocket.<init>(DatagramSocket.java:196)
at org.apache.logging.log4j.core.net.DatagramOutputStream.<init>(DatagramOutputStream.java:72)
... 29 more
2017-12-08 10:45:28,556 ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.SyslogAppender for element Syslog.
2017-12-08 10:45:28,567 ERROR Null object returned for Syslog in Appenders.
2017-12-08 10:45:28,579 ERROR Unable to locate appender SYSLOG for logger
最后,产生了这个输出:
Hello World!
2017-12-08 10:45:28 INFO App:17 - Hello Logger!
此外,写入文件也可以。
如何修复日志记录到syslog的任何线索都将非常感激。