✨ Features
Flexible Configuration
- Customizable log levels and filtering
- Global tag prefixes for organized logging
- Thread-safe operations
- Enable/disable logging globally
Multiple Destinations
- Logcat - Standard Android logging
- File Logging - Persistent file storage
- Custom Destinations - Extensible interface
Pretty Printing
- Beautiful JSON formatting
- XML pretty printing
- Structured log formatting
- Timestamp and thread info
Smart File Management
- Automatic log rotation
- Size-based file clearing
- Organized directory structure
- Crash-safe file operations
🚀 Installation
Kotlin DSL
Groovy
//settings.gradle dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { mavenCentral() maven { url = URI("https://jitpack.io") } // Add this } } // App module dependencies { implementation("com.github.goodluck3301:FlexLogger:flexlogger-1.0") }
//settings.gradle dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { mavenCentral() maven { url 'https://jitpack.io' } // Add this } } // App module dependencies { implementation 'com.github.goodluck3301:FlexLogger:flexlogger-1.0' }
⚡ Quick Start
1. Initialize in your Application class
class MyApplication : Application() { override fun onCreate() { super.onCreate() FlexLogger.init { enabled = true globalTagPrefix = "MyApp" minLevel = LogLevel.DEBUG showTimestamp = true showThreadInfo = true // Add destinations logcat() file( context = this@MyApplication, fileName = "app_log.txt", maxFileSizeMb = 5 ) } } }
2. Register in AndroidManifest.xml
<application android:name=".MyApplication" ... > </application>
3. Start logging!
class MainActivity : AppCompatActivity() { private val TAG = "MainActivity" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Basic logging flexLogI(TAG) { "Activity created successfully" } flexLogD(TAG) { "Debug information: ${someVariable}" } // Error logging with exception try { riskyOperation() } catch (e: Exception) { flexLogE(TAG, e) { "Operation failed" } } // Pretty print JSON flexLogJson(TAG) { """{"user": "John", "id": 123}""" } } }
📚 Documentation
Log Levels
Level | Function | Description |
---|---|---|
VERBOSE |
flexLogV() |
Detailed information for debugging |
DEBUG |
flexLogD() |
Debug information for development |
INFO |
flexLogI() |
General information messages |
WARN |
flexLogW() |
Warning messages for potential issues |
ERROR |
flexLogE() |
Error messages for failures |
ASSERT |
flexLogWtf() |
Critical errors that should never happen |
💡 Examples
Basic Usage
class NetworkManager { private val TAG = "NetworkManager" suspend fun fetchData(): Result<Data> { flexLogI(TAG) { "Starting data fetch operation" } return try { val response = apiService.getData() flexLogJson(TAG, LogLevel.DEBUG) { response.body() } Result.success(response.data) } catch (e: Exception) { flexLogE(TAG, e) { "Failed to fetch data from API" } Result.failure(e) } } }
Custom Log Destination
class RemoteLogDestination(private val apiEndpoint: String) : LogDestination { override val id = "remote_logger" override fun send(logMessage: LogMessage, formattedMessage: String) { // Send logs to remote server if (logMessage.level >= LogLevel.ERROR) { sendToRemoteServer(formattedMessage) } } private fun sendToRemoteServer(message: String) { // Implementation for remote logging } } // Usage in configuration FlexLogger.init { addDestination(RemoteLogDestination("https://api.example.com/logs")) }
🏗️ Architecture
Application
flexLogI(...)
flexLogE(...)
flexLogJson()
flexLogI(...)
flexLogE(...)
flexLogJson()
→
FlexLogger
LogMessage
Processing
LogMessage
Processing
→
Destinations
Logcat
File
Custom
Logcat
File
Custom
🎯 Best Practices
✅ Do's
- Initialize early: Set up FlexLogger in your Application's onCreate()
- Use meaningful tags: Create constants for tags in each class
- Leverage lazy evaluation: Use lambda functions for expensive string operations
- Log at appropriate levels: Use DEBUG for development, INFO for important events
- Handle exceptions: Always log exceptions with context
❌ Don'ts
- Don't log sensitive data: Avoid logging passwords, tokens, or personal information
- Don't over-log: Excessive logging can impact performance
- Don't forget to disable: Consider disabling verbose logging in production
- Don't ignore file sizes: Monitor log file sizes to prevent storage issues
🔒 Production Configuration
FlexLogger.init { enabled = !BuildConfig.DEBUG // Disable in release builds minLevel = LogLevel.INFO // Only log important messages showThreadInfo = false // Reduce log verbosity if (BuildConfig.DEBUG) { logcat() file(context = this@MyApplication, maxFileSizeMb = 5) } }