Skip to main content

Overview

The logger package provides structured logging functionality with colored console output and file logging. It supports both human-readable terminal output and JSON mode for programmatic integration with Tauri or other frontends.

Types

Logger

Main logger type that handles all logging operations.
type Logger struct {
    log        *logrus.Logger
    colors     struct {
        red    *color.Color
        green  *color.Color
        yellow *color.Color
        blue   *color.Color
        purple *color.Color
        cyan   *color.Color
        bold   *color.Color
    }
    logFile    *os.File
    jsonWriter *api.JSONWriter
    jsonMode   bool
}
log
*logrus.Logger
Underlying logrus logger instance
logFile
*os.File
File handle for persistent log storage
jsonWriter
*api.JSONWriter
JSON writer for structured output (when in JSON mode)
jsonMode
bool
Whether logger is in JSON mode for programmatic output

Functions

NewLogger

Creates a new Logger instance with human-readable output.
func NewLogger(logPath string) (*Logger, error)
logPath
string
required
Path to log file (empty string disables file logging)
logger
*Logger
A new Logger instance
error
error
Returns an error if log file creation fails

Example

import "github.com/Azilone/Camera-Workflow/internal/logger"

log, err := logger.NewLogger("/path/to/conversion.log")
if err != nil {
    panic(err)
}
defer log.Close()

NewLoggerWithMode

Creates a new Logger instance with optional JSON mode.
func NewLoggerWithMode(logPath string, jsonMode bool) (*Logger, error)
logPath
string
required
Path to log file (empty string disables file logging)
jsonMode
bool
required
Enable JSON output for programmatic integration
logger
*Logger
A new Logger instance
error
error
Returns an error if log file creation fails

Example

// JSON mode for Tauri integration
log, err := logger.NewLoggerWithMode("/tmp/conversion.log", true)
if err != nil {
    panic(err)
}
defer log.Close()

Methods

Log

Logs a general informational message.
func (l *Logger) Log(message string)
message
string
required
Message to log

Example

log.Log("Starting conversion process")
Output: [10:30:45] Starting conversion process

Info

Logs an informational message with an info icon.
func (l *Logger) Info(message string)
message
string
required
Informational message

Example

log.Info(fmt.Sprintf("Found %d files to process", fileCount))
Output: [i] Found 42 files to process

Success

Logs a success message with a checkmark icon.
func (l *Logger) Success(message string)
message
string
required
Success message

Example

log.Success("Conversion completed successfully")
Output: [✓] Conversion completed successfully

Warn

Logs a warning message with a warning icon.
func (l *Logger) Warn(message string)
message
string
required
Warning message

Example

log.Warn("Skipping disk space check")
Output: [⚠] Skipping disk space check

Error

Logs an error message with error formatting.
func (l *Logger) Error(message string)
message
string
required
Error message

Example

log.Error(fmt.Sprintf("Failed to convert %s: %v", filename, err))
Output: [ERROR 10:30:45] Failed to convert photo.jpg: invalid format

Security

Logs a security-related message with bold red formatting.
func (l *Logger) Security(message string)
message
string
required
Security message

Example

log.Security("Deletion mode activated")
Output: [🔒 SECURITY] Deletion mode activated

ShowHeader

Displays the application header with deletion mode warning.
func (l *Logger) ShowHeader(keepOriginals bool)
keepOriginals
bool
required
Whether originals will be preserved

Example

log.ShowHeader(cfg.KeepOriginals)
Output:
╔══════════════════════════════════════════════════════════════╗
║        Media Converter SECURE v1.0                           ║
╚══════════════════════════════════════════════════════════════╝

🔒 Secure mode: Originals will be preserved

Close

Closes the log file handle.
func (l *Logger) Close() error
error
error
Returns an error if file close fails

Example

defer log.Close()

GetJSONWriter

Returns the JSON writer instance (only available in JSON mode).
func (l *Logger) GetJSONWriter() *api.JSONWriter
jsonWriter
*api.JSONWriter
JSON writer instance or nil if not in JSON mode

IsJSONMode

Checks if logger is in JSON mode.
func (l *Logger) IsJSONMode() bool
jsonMode
bool
Returns true if in JSON mode

Output Modes

Human-Readable Mode (Default)

  • Colored console output with icons
  • Timestamps in 15:04:05 format
  • Multi-writer outputs to both console and log file
  • Pretty formatting with ANSI colors

JSON Mode

  • Structured JSON output to stdout
  • Plain text logging to file
  • Emits JSON events for:
    • Log messages (log event)
    • Progress updates (progress event)
    • Conversion started (started event)
    • Conversion complete (complete event)
    • Statistics (statistics event)

JSON Event Example

{
  "type": "log",
  "timestamp": "2026-03-03T10:30:45Z",
  "level": "info",
  "message": "Starting conversion process"
}

Color Scheme

  • Red: Errors and security warnings
  • Green: Success messages
  • Yellow: Warnings
  • Blue: Timestamps
  • Cyan: Informational messages
  • Purple: Headers

File Logging

When a log path is provided:
  • All messages are written to the log file
  • File format: [LEVEL TIMESTAMP] message
  • Human-readable mode: Logs to both console and file
  • JSON mode: Plain text logs to file, JSON to stdout

Log File Example

[INFO 10:30:45] Starting conversion process
[INFO 10:30:46] Found 42 files to process
[SUCCESS] Conversion completed successfully
[ERROR 10:32:15] Failed to convert photo.jpg: invalid format

Best Practices

  1. Always close the logger: Use defer log.Close() after creation
  2. Use appropriate log levels: Info for status, Warn for issues, Error for failures
  3. Format messages clearly: Include context like file names, counts, errors
  4. Enable JSON mode for GUIs: When integrating with Tauri or other frontends
  • Converter - Uses Logger for conversion output
  • Config - Controls JSON mode via JSONMode flag

Build docs developers (and LLMs) love