From e92dea78ebad3f921c031a974378a218c6bd90b1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 9 Jun 2024 13:37:45 -0700 Subject: Decode text data in logs (fix #5336) --- src/main/java/org/traccar/config/Keys.java | 8 ++++++++ .../handler/network/StandardLoggingHandler.java | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 91d5dac5d..e51ba07c4 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -1901,6 +1901,14 @@ public final class Keys { List.of(KeyType.CONFIG), true); + /** + * If all bytes are printable characters, log network data as text instead of HEX. + */ + public static final ConfigKey LOGGER_TEXT_PROTOCOL = new BooleanConfigKey( + "logger.decodeTextData", + List.of(KeyType.CONFIG), + true); + /** * Log file rotation interval, the default rotation interval is once a day. * This option is ignored if 'logger.rotate' = false diff --git a/src/main/java/org/traccar/handler/network/StandardLoggingHandler.java b/src/main/java/org/traccar/handler/network/StandardLoggingHandler.java index dae93655d..f6ee08756 100644 --- a/src/main/java/org/traccar/handler/network/StandardLoggingHandler.java +++ b/src/main/java/org/traccar/handler/network/StandardLoggingHandler.java @@ -24,21 +24,32 @@ import jakarta.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.NetworkMessage; +import org.traccar.config.Config; +import org.traccar.config.Keys; +import org.traccar.helper.BufferUtil; import org.traccar.helper.NetworkUtil; import org.traccar.model.LogRecord; import org.traccar.session.ConnectionManager; +import java.nio.charset.StandardCharsets; + public class StandardLoggingHandler extends ChannelDuplexHandler { private static final Logger LOGGER = LoggerFactory.getLogger(StandardLoggingHandler.class); private final String protocol; private ConnectionManager connectionManager; + private boolean decodeTextData; public StandardLoggingHandler(String protocol) { this.protocol = protocol; } + @Inject + public void setConfig(Config config) { + decodeTextData = config.getBoolean(Keys.LOGGER_TEXT_PROTOCOL); + } + @Inject public void setConnectionManager(ConnectionManager connectionManager) { this.connectionManager = connectionManager; @@ -66,7 +77,14 @@ public class StandardLoggingHandler extends ChannelDuplexHandler { if (networkMessage.getMessage() instanceof ByteBuf) { LogRecord record = new LogRecord(ctx.channel().localAddress(), networkMessage.getRemoteAddress()); record.setProtocol(protocol); - record.setData(ByteBufUtil.hexDump((ByteBuf) networkMessage.getMessage())); + ByteBuf data = (ByteBuf) networkMessage.getMessage(); + if (decodeTextData && BufferUtil.isPrintable(data, data.readableBytes())) { + record.setData(data.getCharSequence( + data.readerIndex(), data.readableBytes(), StandardCharsets.US_ASCII).toString() + .replace("\r", "\\r").replace("\n", "\\n")); + } else { + record.setData(ByteBufUtil.hexDump(data)); + } return record; } } -- cgit v1.2.3