Skip to content

dronlinepl/drlogger

Repository files navigation

DR-Logger

License: MIT Maven Central Kotlin GitHub

A powerful Kotlin Multiplatform logging library designed for cross-platform applications. DR-Logger provides a flexible, listener-based logging architecture with platform-specific implementations for Android, iOS, JVM, macOS, Linux, and Windows.

Features

  • Multiplatform Support: Single codebase for Android, iOS, JVM, macOS, Linux, and Windows
  • Flexible Listener Architecture: Route log messages to multiple outputs simultaneously
  • Multiple Log Levels: DEBUG, TRACE, INFO, WARN, ERROR, FATAL with configurable filtering
  • Asynchronous Processing: Non-blocking log delivery using Kotlin coroutines
  • Advanced Filtering: Regex-based message and tag filtering
  • Thread-Safe: Built with mutex locks and atomic operations for concurrent access
  • Platform-Specific Integrations:
    • Android: Logcat integration
    • JVM: Apache Log4j support
    • Linux: Systemd logging support
    • iOS/macOS: Native platform logging
    • Windows: Windows Event Log integration
  • Built-in Listeners:
    • Console output with emoji indicators
    • Daily rotating file logs with automatic cleanup
    • Platform-specific system loggers

Installation

Building from Source

Currently, DR-Logger is best used by building from source and publishing to your local Maven repository:

git clone https://github.com/dronlinepl/drlogger-library.git
cd drlogger-library

Then add the dependency to your build.gradle.kts:

repositories {
    mavenLocal()
}

dependencies {
    implementation("pl.dronline.multiplatform.utils:drlogger-library:1.0.+")
}

Quick Start

Basic Usage

import pl.dronline.utils.log.DrLogger
import pl.dronline.utils.log.listener.EmojiConsoleLogListener

// Add a console listener
DrLogger.addListener(EmojiConsoleLogListener().apply {
    enabled = true
})

// Create logger instance
val logger = DrLogger("MyApp")

// Log messages
logger.debug("Application started")
logger.info("User logged in")
logger.warn("Memory usage high")
logger.error("Failed to connect to server")
logger.fatal("Critical system failure")

Short Form Methods

logger.d("TAG", "Debug message")
logger.i("TAG", "Info message")
logger.w("TAG", "Warning message")
logger.e("TAG", "Error message")

Logging Exceptions

try {
    // Some code that might throw
} catch (e: Exception) {
    logger.error(e, "Failed to process request")
}

Custom Listeners

Create custom log listeners by implementing ILogListener:

class CustomLogListener : ALogListener("CustomListener") {
    override suspend fun onLogMessage(message: LogMessage) {
        // Handle log message
        println("[${message.level}] ${message.tag}: ${message.message}")
    }
}

// Add to logger
DrLogger.addListener(CustomLogListener().apply { enabled = true })

File Logging with Daily Rotation

import pl.dronline.utils.log.listener.DailyFileLogListener
import java.io.File

val fileListener = DailyFileLogListener(
    logDir = File("/path/to/logs"),
    filePrefix = "app",
    maxDays = 7  // Keep logs for 7 days
).apply { enabled = true }

DrLogger.addListener(fileListener)

Filtering

// Filter by log level
listener.minLevel = DrLogLevel.WARN

// Filter by tag pattern
listener.tagFilter = Regex("^MyApp.*")

// Filter by message pattern
listener.messageFilter = Regex(".*error.*", RegexOption.IGNORE_CASE)

Platform-Specific Setup

Android

No additional setup required. Logcat integration works out of the box:

DrLogger.addListener(LogcatLogListener().apply { enabled = true })

JVM (Backend/Desktop)

For Log4j integration:

DrLogger.addListener(Log4jLogListener().apply { enabled = true })

Make sure Log4j is configured in your application.

Linux

Install required system dependencies:

sudo apt install libsystemd-dev gcc-multilib

Windows

No additional setup required. Use LogcatLogListener to write to Windows Event Log. View logs using Event Viewer (eventvwr.msc) under "Windows Logs" -> "Application":

DrLogger.addListener(LogcatLogListener().apply { enabled = true })

Log levels are mapped to Event Log types:

  • ERROR -> Error
  • WARN -> Warning
  • INFO/DEBUG/TRACE -> Information

iOS/macOS

No additional setup required. Native platform logging is automatically configured.

Supported Platforms

Platform Target Min Version
Android androidTarget API 24 (Android 7.0)
iOS iosArm64, iosX64, iosSimulatorArm64 iOS 12+
JVM jvm Java 8+
macOS macosArm64, macosX64 macOS 10.13+
Linux linuxX64, linuxArm64 glibc 2.27+
Windows mingwX64 Windows 7+

Architecture

DR-Logger uses a central dispatch system (DrLoggerFactory) that routes log messages to registered listeners. Each listener runs independently with its own coroutine scope, ensuring non-blocking operation.

┌──────────────┐
│   DrLogger   │
└──────┬───────┘
       │
       v
┌──────────────────┐       ┌─────────────────┐
│ DrLoggerFactory  │──────>│  ILogListener   │
└──────────────────┘       └─────────────────┘
       │                            │
       ├────────────────────────────┼──────────────┐
       v                            v              v
┌─────────────┐         ┌────────────────┐  ┌──────────┐
│   Console   │         │   File Logger  │  │  Logcat  │
└─────────────┘         └────────────────┘  └──────────┘

Configuration

Global Settings

// Set minimum log level globally
DrLogger.minLevel = DrLogLevel.INFO

// Enable/disable specific listeners
listener.enabled = true

Buffer Configuration

DR-Logger uses a 512-element buffer for log messages. Messages are processed asynchronously to avoid blocking the calling thread.

Building from Source

Prerequisites

  • JDK 17 or higher (Java 17 is required for the build)
  • Kotlin 2.2.21+ (included via Gradle)
  • Android SDK (for Android targets) - set ANDROID_HOME environment variable
  • Xcode (for iOS/macOS targets)

Platform-Specific Notes

  • iOS/macOS targets can only be built on macOS with Xcode installed
  • Android targets require Android SDK
  • Linux targets can be built on any platform but require libsystemd-dev for native features
  • Windows targets can be built on any platform using MinGW toolchain

License

This project is licensed under the MIT License - see the LICENSE file for details.

Authors

  • DR-ONLINE SP. Z O.O. - Copyright (c) 2017-2025
  • Przemysław Dobrowolski - Copyright (c) 2017-2025

Acknowledgments

Built with:

Support

For questions, issues, or feature requests:

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages