tinylog native logging implementation providing writers, policies, converters, and core logging functionality
Raw writers in tinylog-impl provide low-level byte array writing capabilities and decorators for advanced I/O operations. These components handle the actual file and network I/O operations underlying the higher-level writer implementations.
Core interface for writers that handle raw byte array operations.
/**
* Interface for writers that output raw byte arrays
*/
interface ByteArrayWriter {
/**
* Read trailing bytes from the output destination
* @param buffer Buffer to read into
* @param offset Offset in buffer to start reading
* @param length Maximum number of bytes to read
* @return Number of bytes actually read
* @throws IOException if I/O operation fails
*/
int readTail(byte[] data, int offset, int length) throws IOException;
/**
* Write byte array data
* @param data Data to write
* @param length Number of bytes to write from data
* @throws IOException if I/O operation fails
* @deprecated Use write(byte[], int, int) instead
*/
@Deprecated
void write(byte[] data, int length) throws IOException;
/**
* Write byte array data with offset and length
* @param data Data to write
* @param offset Offset in data to start writing
* @param length Number of bytes to write
* @throws IOException if I/O operation fails
*/
void write(byte[] data, int offset, int length) throws IOException;
/**
* Truncate the output to specified size
* @param size Size to truncate to
* @throws IOException if I/O operation fails
*/
void truncate(int size) throws IOException;
/**
* Flush any buffered data
* @throws IOException if I/O operation fails
*/
void flush() throws IOException;
/**
* Close the writer and release resources
* @throws IOException if I/O operation fails
*/
void close() throws IOException;
}Provides random access file operations for log file management.
/**
* Writer for random access file operations
*/
class RandomAccessFileWriter implements ByteArrayWriter {
/**
* Create random access file writer
* @param file Underlying random access file
*/
public RandomAccessFileWriter(RandomAccessFile file);
}Thread-safe random access file writer with file locking support.
/**
* Thread-safe random access file writer with locking
*/
class LockedRandomAccessFileWriter implements ByteArrayWriter {
/**
* Create locked file writer
* @param file Underlying random access file
*/
public LockedRandomAccessFileWriter(RandomAccessFile file);
}Decorators provide additional functionality by wrapping other ByteArrayWriter implementations.
Adds buffering capabilities to improve I/O performance.
/**
* Decorator that adds buffering to byte array writers
*/
class BufferedWriterDecorator implements ByteArrayWriter {
/**
* Create buffered writer with default buffer size
* @param writer Underlying writer to decorate
*/
public BufferedWriterDecorator(ByteArrayWriter writer);
/**
* Create buffered writer with custom buffer size
* @param writer Underlying writer to decorate
* @param bufferSize Size of internal buffer in bytes
*/
public BufferedWriterDecorator(ByteArrayWriter writer, int bufferSize);
/**
* Check if auto-flush is enabled
* @return true if auto-flush is enabled
*/
public boolean isAutoFlush();
/**
* Set auto-flush behavior
* @param autoFlush Whether to auto-flush after each write
*/
public void setAutoFlush(boolean autoFlush);
}Handles character encoding adjustments for text data.
/**
* Decorator for character encoding adjustments
*/
class CharsetAdjustmentWriterDecorator implements ByteArrayWriter {
/**
* Create charset adjustment decorator with default encoding
* @param writer Underlying writer to decorate
*/
public CharsetAdjustmentWriterDecorator(ByteArrayWriter writer);
/**
* Create charset adjustment decorator with specific encoding
* @param writer Underlying writer to decorate
* @param charset Character encoding to use
*/
public CharsetAdjustmentWriterDecorator(ByteArrayWriter writer, Charset charset);
/**
* Get the configured charset
* @return Character encoding
*/
public Charset getCharset();
}Provides thread-safe access to underlying writers through synchronization.
/**
* Decorator that provides thread-safe access through synchronization
*/
class SynchronizedWriterDecorator implements ByteArrayWriter {
/**
* Create synchronized writer decorator
* @param writer Underlying writer to decorate
*/
public SynchronizedWriterDecorator(ByteArrayWriter writer);
/**
* Get the underlying writer (for testing/debugging)
* @return Wrapped writer instance
*/
public ByteArrayWriter getUnderlyingWriter();
}Abstract and concrete implementations for network-based log output.
Base class for network socket writers.
/**
* Abstract base class for socket-based writers
*/
abstract class AbstractSocketWriter extends AbstractFormatPatternWriter {
/**
* Create socket writer with configuration
* @param properties Configuration properties
* @throws IOException if network connection fails
*/
protected AbstractSocketWriter(Map<String, String> properties) throws IOException;
/**
* Get the remote host address
* @return Host address string
*/
public String getHost();
/**
* Get the remote port number
* @return Port number
*/
public int getPort();
/**
* Check if connection is currently active
* @return true if connected
*/
public boolean isConnected();
/**
* Reconnect to the remote host
* @throws IOException if reconnection fails
*/
public void reconnect() throws IOException;
}TCP-based network writer for reliable log transmission.
/**
* Writer for TCP socket output
*/
class TcpSocketWriter extends AbstractSocketWriter {
/**
* Create TCP socket writer
* @param properties Configuration properties (host, port, etc.)
* @throws IOException if TCP connection fails
*/
public TcpSocketWriter(Map<String, String> properties) throws IOException;
/**
* Get socket timeout value
* @return Timeout in milliseconds
*/
public int getSocketTimeout();
/**
* Set socket timeout
* @param timeout Timeout in milliseconds
*/
public void setSocketTimeout(int timeout);
/**
* Check if TCP keep-alive is enabled
* @return true if keep-alive is enabled
*/
public boolean isKeepAlive();
}UDP-based network writer for fast, connectionless log transmission.
/**
* Writer for UDP socket output
*/
class UdpSocketWriter extends AbstractSocketWriter {
/**
* Create UDP socket writer
* @param properties Configuration properties (host, port, etc.)
* @throws IOException if UDP socket creation fails
*/
public UdpSocketWriter(Map<String, String> properties) throws IOException;
/**
* Get maximum packet size
* @return Maximum UDP packet size in bytes
*/
public int getMaxPacketSize();
/**
* Set maximum packet size
* @param maxSize Maximum UDP packet size in bytes
*/
public void setMaxPacketSize(int maxSize);
}Enumerations and utilities for Syslog protocol support.
Enumeration of standard Syslog facility codes.
/**
* Enumeration of Syslog facility codes
*/
enum SyslogFacility {
/** Kernel messages */
KERN(0),
/** User-level messages */
USER(1),
/** Mail system messages */
MAIL(2),
/** System daemon messages */
DAEMON(3),
/** Security/authorization messages */
AUTH(4),
/** Internal syslogd messages */
SYSLOG(5),
/** Line printer subsystem */
LPR(6),
/** Network news subsystem */
NEWS(7),
/** UUCP subsystem */
UUCP(8),
/** Clock daemon */
CRON(9),
/** Security/authorization messages */
AUTHPRIV(10),
/** FTP daemon */
FTP(11),
/** NTP subsystem */
NTP(12),
/** Log audit */
SECURITY(13),
/** Log alert */
CONSOLE(14),
/** Clock daemon */
CLOCK(15),
/** Local use 0-7 */
LOCAL0(16), LOCAL1(17), LOCAL2(18), LOCAL3(19),
LOCAL4(20), LOCAL5(21), LOCAL6(22), LOCAL7(23);
/**
* Get facility code value
* @return Numeric facility code
*/
public int getCode();
}Enumeration of standard Syslog severity levels.
/**
* Enumeration of Syslog severity levels
*/
enum SyslogSeverity {
/** System is unusable */
EMERGENCY(0),
/** Action must be taken immediately */
ALERT(1),
/** Critical conditions */
CRITICAL(2),
/** Error conditions */
ERROR(3),
/** Warning conditions */
WARNING(4),
/** Normal but significant condition */
NOTICE(5),
/** Informational messages */
INFORMATIONAL(6),
/** Debug-level messages */
DEBUG(7);
/**
* Get severity code value
* @return Numeric severity code
*/
public int getCode();
}File-Based Raw Writers:
import org.tinylog.writers.raw.*;
// Basic random access file writing
RandomAccessFileWriter fileWriter = new RandomAccessFileWriter("output.log", true);
byte[] data = "Log entry\n".getBytes(StandardCharsets.UTF_8);
fileWriter.write(data, 0, data.length);
fileWriter.flush();
fileWriter.close();
// Locked file writing for shared access
LockedRandomAccessFileWriter lockedWriter = new LockedRandomAccessFileWriter("shared.log", true);
lockedWriter.write(data, 0, data.length);
lockedWriter.close();Decorated Writers:
// Buffered writing for performance
ByteArrayWriter baseWriter = new RandomAccessFileWriter("buffered.log");
BufferedWriterDecorator bufferedWriter = new BufferedWriterDecorator(baseWriter, 8192);
bufferedWriter.setAutoFlush(false);
// Thread-safe writing
SynchronizedWriterDecorator syncWriter = new SynchronizedWriterDecorator(bufferedWriter);
// Charset-aware writing
CharsetAdjustmentWriterDecorator charsetWriter =
new CharsetAdjustmentWriterDecorator(syncWriter, StandardCharsets.UTF_8);Network Writers Configuration:
# TCP socket writer
writer=syslog
writer.host=log-server.example.com
writer.port=514
writer.protocol=tcp
writer.facility=daemon
# UDP socket writer with custom settings
writer=syslog
writer.host=192.168.1.100
writer.port=514
writer.protocol=udp
writer.facility=local0
writer.format={level}: {message}Complex Writer Stack:
// Create a complex writer stack: File -> Buffered -> Charset -> Synchronized
ByteArrayWriter writer = new RandomAccessFileWriter("complex.log", true);
writer = new BufferedWriterDecorator(writer, 16384);
writer = new CharsetAdjustmentWriterDecorator(writer, StandardCharsets.UTF_8);
writer = new SynchronizedWriterDecorator(writer);
// Use the decorated writer
String logMessage = "Complex log entry";
byte[] messageBytes = logMessage.getBytes(StandardCharsets.UTF_8);
writer.write(messageBytes, 0, messageBytes.length);
writer.flush();Syslog Integration:
import org.tinylog.writers.raw.SyslogFacility;
import org.tinylog.writers.raw.SyslogSeverity;
// Map log levels to syslog severities
SyslogSeverity severity = SyslogSeverity.fromLevel(Level.ERROR);
SyslogFacility facility = SyslogFacility.DAEMON;
// Calculate syslog priority value (facility * 8 + severity)
int priority = facility.getCode() * 8 + severity.getCode();Performance Considerations:
# High-performance file writing
writer.buffered=true
writer.writingthread=true
# Use appropriate buffer sizes
writer.buffer.size=32768
# Network settings for reliability
writer.tcp.keepalive=true
writer.tcp.timeout=30000Install with Tessl CLI
npx tessl i tessl/maven-org-tinylog--tinylog-impl