sarkasronie
sarkasronie

Reputation: 345

How to enable internal logging of netty?

I tried to enable netty-logging in my project. There are a lot of examples in the internet - but at the end they doesn't work for me.

InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);

and

p.addLast("logger", new LoggingHandler(LogLevel.DEBUG));

should do the trick...

public final class EchoServer {
static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));

public static void main(String[] args) throws Exception {
    InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
    // Configure SSL.
    final SslContext sslCtx;
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
            .channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, 100)
            .childHandler(new ChannelInitializer < SocketChannel > () {
                @Override
                public void initChannel(SocketChannel ch)
                throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    if (sslCtx != null) {
                        p.addLast(sslCtx.newHandler(ch.alloc()));
                    }
                    p.addLast(new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(getClass().getClassLoader())));
                    p.addLast(new ObjectEncoder());
                    p.addLast("logger", new LoggingHandler(LogLevel.DEBUG));
                    p.addLast(new EchoServerHandler());
                }
            });

        // Start the server.
        ChannelFuture f = b.bind(PORT).sync();

        System.out.println("EchoServer listening at " + EchoServer.PORT);

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
  }
}

Does anyone have an idea how to get it working?

Upvotes: 3

Views: 15615

Answers (2)

adramazany
adramazany

Reputation: 664

you could use LoggingHandler for getting netty's logs:

b.group(bossGroup, workerGroup)
 .channel(NioServerSocketChannel.class)
 .handler(new LoggingHandler(LogLevel.INFO))
 ...

Upvotes: 3

sarkasronie
sarkasronie

Reputation: 345

Solved for me. If you use JdkLoggerFactory, have in mind that logging.properties out of jre\lib\ is used. The default of JUL is:

# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers.  For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level= INFO

We generated our own property-file so that we can set the level dynamically.

Upvotes: 2

Related Questions