Crate canlog

Source
Expand description

This crate extends ic_canister_log to provide native support for log priority levels, filtering and sorting.

The main functionality is provided by the LogPriorityLevels and GetLogFilter traits as well as the log macro.

Custom log priority levels may be defined by declaring an enum and implementing the LogPriorityLevels trait for it, usually through the derive annotation available with the derive feature of canlog.

Additionally, log filtering may be achieved by implementing the GetLogFilter trait on the enum defining the log priorities.

  • Example:
use canlog::{GetLogFilter, LogFilter, LogPriorityLevels, log};

#[derive(LogPriorityLevels)]
enum LogPriority {
    #[log_level(capacity = 100, name = "INFO")]
    Info,
    #[log_level(capacity = 500, name = "DEBUG")]
    Debug,
}

impl GetLogFilter for LogPriority {
    fn get_log_filter() -> LogFilter {
        LogFilter::ShowAll
    }
}

fn main() {
    log!(LogPriority::Info, "Some rather important message.");
    log!(LogPriority::Debug, "Some less important message.");
}

Expected Output:

2025-02-26 08:27:10 UTC: [Canister lxzze-o7777-77777-aaaaa-cai] INFO main.rs:13 Some rather important message.
2025-02-26 08:27:10 UTC: [Canister lxzze-o7777-77777-aaaaa-cai] DEBUG main.rs:14 Some less important message.

Macros§

declare_log_buffer
Declares a new canister log buffer.
log
Wrapper for the ic_canister_log::log macro that allows logging for a given variant of an enum implementing the LogPriorityLevels trait. See the example in the crate documentation.
raw_log
Adds a new record to a canister log buffer.

Structs§

Log
A container for log entries at a given log priority level.
LogEntry
A single log entry.

Enums§

LogFilter
Only log entries matching this filter will be recorded.
Sort
Defines a sorting order for log entries

Traits§

GetLogFilter
Returns the LogFilter to check what entries to record. This trait should be implemented manually.
LogPriorityLevels
Represents a log priority level. This trait is meant to be implemented automatically with the derive attribute macro which is available with the derive feature of this crate.
Sink

Functions§

export_logs
Exports the contents of a buffer as a vector of entries in the order of insertion.

Type Aliases§

GlobalBuffer