We would like to use a log server and send log events to it. We would like to use SimpleSocketServer to avoid the SSL setup overhead. See below for your test pom.file (original) (raw)

We would like to use a log server and send log events to it. We would like to use SimpleSocketServer to avoid the SSL setup overhead. See below for your test pom.file

On the client we use SocketAppender with the following logback.xml configuration.

<configuration>
  <appender name="SOCKET" class="ch.qos.logback.classic.net.SocketAppender">
    <remoteHost>localhost</remoteHost>
    <port>6001</port>
    <reconnectionDelay>10000</reconnectionDelay> <includeCallerData>true</includeCallerData>
  </appender>

  <root level="INFO">
    <appender-ref ref="SOCKET" />
  </root>
</configuration>

We use this for testing:

        // Testing error log
        MDC.put("user", "THIS IS THE USER");
        LOG.error("LARS TESTET DAS OLLE ERROR LOG ");

In LoggingEvent, the data is prepared here:

    public Map<String, String> getMDCPropertyMap() {
        // populate mdcPropertyMap if null
        if (mdcPropertyMap == null) {
            MDCAdapter mdcAdapter = loggerContext.getMDCAdapter();
            if (mdcAdapter instanceof LogbackMDCAdapter)
                mdcPropertyMap = ((LogbackMDCAdapter) mdcAdapter).getPropertyMap();
            else
                mdcPropertyMap = mdcAdapter.getCopyOfContextMap();
        }
        // mdcPropertyMap still null, use emptyMap()
        if (mdcPropertyMap == null)
            mdcPropertyMap = Collections.emptyMap();

        return mdcPropertyMap;
    }

But the map is empty

Any advice how we can send this data and or the MDC data to the server?

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yourcompany.logging</groupId>
    <artifactId>central-log-server</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <logback.version>1.5.26</logback.version> </properties>

    <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>ch.qos.logback.classic.net.SimpleSocketServer</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Originally posted by @vogella in #1008