Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Custom Log Levels

Add your own log levels with custom priorities and colors.

Overview

Beyond the 8 built-in levels, you can create custom levels for specific use cases.

Adding Custom Levels

#![allow(unused)]
fn main() {
use logly::prelude::*;

let logger = Logger::new();
logger.add_sink(SinkConfig::default())?;

// Add custom level: name, priority, ANSI color code
logger.add_custom_level("NOTICE".to_string(), 35, "96".to_string())?;
logger.add_custom_level("AUDIT".to_string(), 42, "95".to_string())?;
}

Using Custom Levels

#![allow(unused)]
fn main() {
logger.log_custom("NOTICE", "Important notice".to_string())?;
logger.log_custom("AUDIT", "Security audit log".to_string())?;
}

Priority System

Custom levels fit into the priority hierarchy:

LevelPriority
TRACE5
DEBUG10
INFO20
SUCCESS25
WARNING30
NOTICE35 (custom)
ERROR40
AUDIT42 (custom)
FAIL45
CRITICAL50

ANSI Color Codes

Common color codes:

  • 30 - Black
  • 31 - Red
  • 32 - Green
  • 33 - Yellow
  • 34 - Blue
  • 35 - Magenta
  • 36 - Cyan
  • 37 - White
  • 90-97 - Bright colors

Examples

Security Audit Level

#![allow(unused)]
fn main() {
logger.add_custom_level("AUDIT".to_string(), 42, "95".to_string())?;
logger.log_custom("AUDIT", "User login attempt".to_string())?;
}

Performance Metrics

#![allow(unused)]
fn main() {
logger.add_custom_level("METRIC".to_string(), 15, "36".to_string())?;
logger.log_custom("METRIC", "Response time: 45ms".to_string())?;
}

Business Events

#![allow(unused)]
fn main() {
logger.add_custom_level("BUSINESS".to_string(), 28, "33".to_string())?;
logger.log_custom("BUSINESS", "Order placed: $99.99".to_string())?;
}

See Also