Search code examples
log4josgilog4j2apache-karafjbossfuse

SocketAppender in OSGi / Fuse 7


Using Fuse 7 (Karaf version 4.2.0.fuse-000237-redhat-1)

Trying to get SocketAppender working. Best guess was the following, added to org.ops4j.pax.logging.cfg

# ELK appender
log4j2.appender.elk.type = SocketAppender
log4j2.appender.elk.name = ELK
log4j2.appender.elk.remoteHost=10.0.0.2
log4j2.appender.elk.port=12202
log4j2.appender.elk.application=QA
log4j2.appender.elk.reconnectionDelay=0
log4j2.rootLogger.appenderRef.ELK.ref = ELK

But that just stops all logs, even the console.

I'm actually trying to get the logstash-gelf appender working, but using a custom appender in Fuse 7 / Karaf looks like a total mess. (See link). I doubt it would ever work.

SocketAppender was working fine with log4j in Fuse 6.
Main difference in the setup was this line, which isn't applicable to log4j2 now:

log4j.throwableRenderer=org.apache.log4j.OsgiThrowableRenderer

------------- EDIT:

    # ELK appender
log4j2.appender.elk.type = Socket
log4j2.appender.elk.name = ELK
log4j2.appender.elk.layout.type = GelfLayout
log4j2.appender.elk.remoteHost=10.0.0.2
log4j2.appender.elk.port=12202
log4j2.appender.elk.application=QA
log4j2.appender.elk.reconnectionDelay=0
log4j2.rootLogger.appenderRef.ELK.ref = ELK

This isn't killing the logs at least. Also found GelfLayout, which might be able to replace the logstash-gelf appender, if it was working.

------------- EDIT 2 ---- Reply to Grzegorz:

Thanks for the reply... unfortunately not working still... I've changed the lines to the following, as described:

# ELK appender
log4j2.appender.socket.type = Socket
log4j2.appender.socket.name = Socket
log4j2.appender.socket.host=localhost
log4j2.appender.socket.port=12202
log4j2.appender.socket.protocol=TCP
log4j2.appender.socket.layout.type = JsonLayout

log4j2.rootLogger.appenderRef.Socket.ref = Socket

And start running netcat

[root@ldkr03 etc]# nc -lk 12202
(not showing anything...)

The log:log test shows 16:10:34.747 INFO [pipe-log:log test] test but doesn't show anything in the nc -lk 12202 window.

Also, maybe relevant, though it writes to the Console just fine:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </dependency>

------------- EDIT 3 ----

Switched to:

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
    </dependency>

But still no.


Solution

  • I used this configuration for appender:

    log4j2.appender.socket.type = Socket
    log4j2.appender.socket.name = Socket
    log4j2.appender.socket.host = localhost
    log4j2.appender.socket.port = 5666
    log4j2.appender.socket.protocol = TCP
    log4j2.appender.socket.layout.type = JsonLayout
    

    and this to attach this appender to root logger:

    log4j2.rootLogger.appenderRef.Socket.ref = Socket
    

    I started nc using nc -lk 5666 and produced log entries using log:log asd from Karaf shell console. Netcat was printing something like this:

    {
      "timeMillis" : 1549546648409,
      "thread" : "pipe-log:log asd",
      "level" : "INFO",
      "loggerName" : "org.apache.karaf.log.core",
      "message" : "asd",
      "endOfBatch" : false,
      "loggerFqcn" : "org.ops4j.pax.logging.log4j2.internal.PaxLoggingServiceImpl",
      "threadId" : 172,
      "threadPriority" : 5
    }
    

    If you check https://logging.apache.org/log4j/2.x/manual/appenders.html#SocketAppender there's no remoteHost property, but host.