Imported from GitHub samson-media/barn#22 — opened by @daniel-samson on 2026-01-21 12:43:20 UTC
Set up SLF4J logging with Logback, including structured logging and log rotation.
docs/config.md
docs/jobs.md
src/main/resources/logback.xml:
src/main/resources/logback.xml
<configuration> <!-- Console appender for development/foreground --> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- File appender for daemon mode --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${BARN_LOG_DIR:-/tmp/barn/logs}/barn.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${BARN_LOG_DIR:-/tmp/barn/logs}/barn.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <maxFileSize>10MB</maxFileSize> <maxHistory>7</maxHistory> <totalSizeCap>100MB</totalSizeCap> </rollingPolicy> <encoder> <pattern>%d{ISO8601} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- JSON appender for structured logging --> <appender name="JSON" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${BARN_LOG_DIR:-/tmp/barn/logs}/barn.json.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${BARN_LOG_DIR:-/tmp/barn/logs}/barn.json.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <maxFileSize>10MB</maxFileSize> <maxHistory>7</maxHistory> </rollingPolicy> <encoder class="net.logstash.logback.encoder.LogstashEncoder"/> </appender> <root level="${BARN_LOG_LEVEL:-INFO}"> <appender-ref ref="CONSOLE"/> <appender-ref ref="FILE"/> </root> </configuration>
public class LoggingConfig { public static void setLogLevel(LogLevel level) { LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.setLevel(Level.valueOf(level.name())); } public static LogLevel getLogLevel() { LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME); return LogLevel.valueOf(rootLogger.getLevel().toString()); } }
public class JobLogEvents { private static final Logger LOG = LoggerFactory.getLogger(JobLogEvents.class); public static void jobCreated(Job job) { LOG.info("Job created: id={} command={} tag={}", job.id(), job.command(), job.tag()); } public static void jobStarted(Job job) { LOG.info("Job started: id={} pid={}", job.id(), job.pid()); } public static void jobCompleted(Job job, Duration duration) { if (job.exitCode() == 0) { LOG.info("Job succeeded: id={} duration={}ms", job.id(), duration.toMillis()); } else { LOG.warn("Job failed: id={} exitCode={} error={}", job.id(), job.exitCode(), job.error()); } } public static void jobRetryScheduled(Job job, Instant retryAt) { LOG.info("Job retry scheduled: id={} attempt={} retryAt={}", job.id(), job.retryCount() + 1, retryAt); } }
Add context to all log messages within a job:
public class JobContext implements AutoCloseable { public static JobContext forJob(Job job) { MDC.put("jobId", job.id()); MDC.put("jobTag", job.tag()); return new JobContext(); } @Override public void close() { MDC.remove("jobId"); MDC.remove("jobTag"); } } // Usage try (var ctx = JobContext.forJob(job)) { LOG.info("Processing started"); // Automatically includes jobId }
/tmp/barn/logs/barn.log
/tmp/barn/jobs/<id>/logs/stdout.log
/tmp/barn/jobs/<id>/logs/stderr.log
LoggingConfigTest.java
JobLogEventsTest.java
mvn verify
Add logstash-logback-encoder for JSON logging:
<dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>8.0</version> </dependency>
Ensure log directory exists on startup:
Files.createDirectories(Path.of(System.getProperty("BARN_LOG_DIR", "/tmp/barn/logs")));
@daniel-samson commented on 2026-01-21 15:34:30 UTC:
Completed SLF4J logging with Logback implementation:
LoggingConfig.java: Runtime log level configuration
setLogLevel()
getLogLevel()
ensureLogDirectory()
getDefaultLogDirectory()
JobLogEvents.java: Structured logging for job lifecycle
jobCreated()
jobStarted()
jobCompleted()
jobRetryScheduled()
jobCancelled()
jobTimedOut()
jobCleaned()
JobContext.java: MDC wrapper for job-scoped logging
No dependencies set.
The note is not visible to the blocked user.
Imported from GitHub samson-media/barn#22 — opened by @daniel-samson on 2026-01-21 12:43:20 UTC
Overview
Set up SLF4J logging with Logback, including structured logging and log rotation.
Context
docs/config.md(log_level setting),docs/jobs.md(logs directory)Requirements
1. Logback Configuration
src/main/resources/logback.xml:2. Runtime Log Level Configuration
3. Structured Log Events
4. MDC (Mapped Diagnostic Context)
Add context to all log messages within a job:
5. Log File Locations
/tmp/barn/logs/barn.log/tmp/barn/jobs/<id>/logs/stdout.log/tmp/barn/jobs/<id>/logs/stderr.log6. Tests Required
LoggingConfigTest.java: Test level changesJobLogEventsTest.java: Test structured eventsAcceptance Criteria
mvn verifypassesImplementation Notes
Add logstash-logback-encoder for JSON logging:
Ensure log directory exists on startup:
Definition of Done
@daniel-samson commented on 2026-01-21 15:34:30 UTC:
Completed SLF4J logging with Logback implementation:
Changes
Configuration
New Files
LoggingConfig.java: Runtime log level configuration
setLogLevel()/getLogLevel()for dynamic level changesensureLogDirectory()for directory setupgetDefaultLogDirectory()with env var supportJobLogEvents.java: Structured logging for job lifecycle
jobCreated(),jobStarted(),jobCompleted()jobRetryScheduled(),jobCancelled(),jobTimedOut()jobCleaned()JobContext.java: MDC wrapper for job-scoped logging
Dependencies
Tests
Test Results