- java.lang.Object
-
- io.netty5.handler.logging.LoggingHandler
-
- All Implemented Interfaces:
ChannelHandler
public class LoggingHandler extends Object implements ChannelHandler
AChannelHandler
that logs all events using a logging framework. By default, all events are logged at DEBUG level and full hex dumps are recorded for ByteBufs.
-
-
Field Summary
Fields Modifier and Type Field Description protected InternalLogLevel
internalLevel
protected InternalLogger
logger
-
Constructor Summary
Constructors Constructor Description LoggingHandler()
Creates a new instance whose logger name is the fully qualified class name of the instance with hex dump enabled.LoggingHandler(BufferFormat format)
Creates a new instance whose logger name is the fully qualified class name of the instance.LoggingHandler(LogLevel level)
Creates a new instance whose logger name is the fully qualified class name of the instance.LoggingHandler(LogLevel level, BufferFormat bufferFormat)
Creates a new instance whose logger name is the fully qualified class name of the instance.LoggingHandler(Class<?> clazz)
Creates a new instance with the specified logger name and with hex dump enabled.LoggingHandler(Class<?> clazz, LogLevel level)
Creates a new instance with the specified logger name.LoggingHandler(Class<?> clazz, LogLevel level, BufferFormat bufferFormat)
Creates a new instance with the specified logger name.LoggingHandler(String name)
Creates a new instance with the specified logger name using the default log level.LoggingHandler(String name, LogLevel level)
Creates a new instance with the specified logger name.LoggingHandler(String name, LogLevel level, BufferFormat bufferFormat)
Creates a new instance with the specified logger name.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Future<Void>
bind(ChannelHandlerContext ctx, SocketAddress localAddress)
Called once a bind operation is made.BufferFormat
bufferFormat()
Returns theBufferFormat
that this handler uses to logvoid
channelActive(ChannelHandlerContext ctx)
TheChannel
of theChannelHandlerContext
is now activevoid
channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause)
Gets called if aThrowable
was thrown when handling inbound events.void
channelInactive(ChannelHandlerContext ctx)
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.void
channelInboundEvent(ChannelHandlerContext ctx, Object evt)
Gets called if a custom inbound event happened.void
channelRead(ChannelHandlerContext ctx, Object msg)
Invoked when the currentChannel
has read a message from the peer.void
channelReadComplete(ChannelHandlerContext ctx)
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
.void
channelRegistered(ChannelHandlerContext ctx)
void
channelUnregistered(ChannelHandlerContext ctx)
void
channelWritabilityChanged(ChannelHandlerContext ctx)
Gets called once the writable state of aChannel
changed.Future<Void>
close(ChannelHandlerContext ctx)
Called once a close operation is made.Future<Void>
connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress)
Called once a connect operation is made.Future<Void>
deregister(ChannelHandlerContext ctx)
Called once a deregister operation is made from the current registeredEventLoop
.Future<Void>
disconnect(ChannelHandlerContext ctx)
Called once a disconnect operation is made.void
flush(ChannelHandlerContext ctx)
Called once a flush operation is made.protected String
format(ChannelHandlerContext ctx, String eventName)
Formats an event and returns the formatted message.protected String
format(ChannelHandlerContext ctx, String eventName, Object arg)
Formats an event and returns the formatted message.protected String
format(ChannelHandlerContext ctx, String eventName, Object firstArg, Object secondArg)
Formats an event and returns the formatted message.boolean
isSharable()
Returnstrue
if this handler is sharable and thus can be added to more than oneChannelPipeline
.LogLevel
level()
Returns theLogLevel
that this handler uses to logFuture<Void>
write(ChannelHandlerContext ctx, Object msg)
Called once a write operation is made.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
channelShutdown, handlerAdded, handlerRemoved, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Field Detail
-
logger
protected final InternalLogger logger
-
internalLevel
protected final InternalLogLevel internalLevel
-
-
Constructor Detail
-
LoggingHandler
public LoggingHandler()
Creates a new instance whose logger name is the fully qualified class name of the instance with hex dump enabled.
-
LoggingHandler
public LoggingHandler(BufferFormat format)
Creates a new instance whose logger name is the fully qualified class name of the instance.- Parameters:
format
- Format of ByteBuf dumping
-
LoggingHandler
public LoggingHandler(LogLevel level)
Creates a new instance whose logger name is the fully qualified class name of the instance.- Parameters:
level
- the log level
-
LoggingHandler
public LoggingHandler(LogLevel level, BufferFormat bufferFormat)
Creates a new instance whose logger name is the fully qualified class name of the instance.- Parameters:
level
- the log levelbufferFormat
- the ByteBuf format
-
LoggingHandler
public LoggingHandler(Class<?> clazz)
Creates a new instance with the specified logger name and with hex dump enabled.- Parameters:
clazz
- the class type to generate the logger for
-
LoggingHandler
public LoggingHandler(Class<?> clazz, LogLevel level)
Creates a new instance with the specified logger name.- Parameters:
clazz
- the class type to generate the logger forlevel
- the log level
-
LoggingHandler
public LoggingHandler(Class<?> clazz, LogLevel level, BufferFormat bufferFormat)
Creates a new instance with the specified logger name.- Parameters:
clazz
- the class type to generate the logger forlevel
- the log levelbufferFormat
- the ByteBuf format
-
LoggingHandler
public LoggingHandler(String name)
Creates a new instance with the specified logger name using the default log level.- Parameters:
name
- the name of the class to use for the logger
-
LoggingHandler
public LoggingHandler(String name, LogLevel level)
Creates a new instance with the specified logger name.- Parameters:
name
- the name of the class to use for the loggerlevel
- the log level
-
LoggingHandler
public LoggingHandler(String name, LogLevel level, BufferFormat bufferFormat)
Creates a new instance with the specified logger name.- Parameters:
name
- the name of the class to use for the loggerlevel
- the log levelbufferFormat
- the ByteBuf format
-
-
Method Detail
-
isSharable
public boolean isSharable()
Description copied from interface:ChannelHandler
Returnstrue
if this handler is sharable and thus can be added to more than oneChannelPipeline
. By default, this method returnsfalse
. If this method returnsfalse
, you have to create a new handler instance every time you add it to a pipeline because it has unshared state such as member variables.- Specified by:
isSharable
in interfaceChannelHandler
-
bufferFormat
public BufferFormat bufferFormat()
Returns theBufferFormat
that this handler uses to log
-
channelRegistered
public void channelRegistered(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
- Specified by:
channelRegistered
in interfaceChannelHandler
- Throws:
Exception
-
channelUnregistered
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
- Specified by:
channelUnregistered
in interfaceChannelHandler
- Throws:
Exception
-
channelActive
public void channelActive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
TheChannel
of theChannelHandlerContext
is now active- Specified by:
channelActive
in interfaceChannelHandler
- Throws:
Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.- Specified by:
channelInactive
in interfaceChannelHandler
- Throws:
Exception
-
channelExceptionCaught
public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
Description copied from interface:ChannelHandler
Gets called if aThrowable
was thrown when handling inbound events.- Specified by:
channelExceptionCaught
in interfaceChannelHandler
- Throws:
Exception
-
channelInboundEvent
public void channelInboundEvent(ChannelHandlerContext ctx, Object evt) throws Exception
Description copied from interface:ChannelHandler
Gets called if a custom inbound event happened.- Specified by:
channelInboundEvent
in interfaceChannelHandler
- Throws:
Exception
-
bind
public Future<Void> bind(ChannelHandlerContext ctx, SocketAddress localAddress)
Description copied from interface:ChannelHandler
Called once a bind operation is made.- Specified by:
bind
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the bind operation is madelocalAddress
- theSocketAddress
to which it should bound- Returns:
- the
Future
which will be notified once the operation completes.
-
connect
public Future<Void> connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress)
Description copied from interface:ChannelHandler
Called once a connect operation is made.- Specified by:
connect
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the connect operation is maderemoteAddress
- theSocketAddress
to which it should connectlocalAddress
- theSocketAddress
which is used as source on connect- Returns:
- the
Future
which will be notified once the operation completes.
-
disconnect
public Future<Void> disconnect(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a disconnect operation is made.- Specified by:
disconnect
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the disconnect operation is made- Returns:
- the
Future
which will be notified once the operation completes.
-
close
public Future<Void> close(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a close operation is made.- Specified by:
close
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the close operation is made- Returns:
- the
Future
which will be notified once the operation completes.
-
deregister
public Future<Void> deregister(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a deregister operation is made from the current registeredEventLoop
.- Specified by:
deregister
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the deregister operation is made- Returns:
- the
Future
which will be notified once the operation completes.
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
. IfChannelOption.AUTO_READ
is off, no further attempt to read an inbound data from the currentChannel
will be made untilChannelHandlerContext.read()
is called.- Specified by:
channelReadComplete
in interfaceChannelHandler
- Throws:
Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
Description copied from interface:ChannelHandler
Invoked when the currentChannel
has read a message from the peer.- Specified by:
channelRead
in interfaceChannelHandler
- Throws:
Exception
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandler
Called once a write operation is made. The write operation will write the messages through theChannelPipeline
. Those are then ready to be flushed to the actualChannel
onceChannel.flush()
is called.- Specified by:
write
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to write- Returns:
- the
Future
which will be notified once the operation completes.
-
channelWritabilityChanged
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called once the writable state of aChannel
changed. You can check the state withChannel.writableBytes()
orChannel.isWritable()
.- Specified by:
channelWritabilityChanged
in interfaceChannelHandler
- Throws:
Exception
-
flush
public void flush(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a flush operation is made. The flush operation will try to flush out all previous written messages that are pending.- Specified by:
flush
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the flush operation is made
-
format
protected String format(ChannelHandlerContext ctx, String eventName)
Formats an event and returns the formatted message.- Parameters:
eventName
- the name of the event
-
format
protected String format(ChannelHandlerContext ctx, String eventName, Object arg)
Formats an event and returns the formatted message.- Parameters:
eventName
- the name of the eventarg
- the argument of the event
-
format
protected String format(ChannelHandlerContext ctx, String eventName, Object firstArg, Object secondArg)
Formats an event and returns the formatted message. This method is currently only used for formattingChannelHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress)
.- Parameters:
eventName
- the name of the eventfirstArg
- the first argument of the eventsecondArg
- the second argument of the event
-
-