Skip to main content

file_logging/
file_logging.rs

1// =============================================================================
2//        #######
3//     ###       ###     F: file_logging.rs
4//    ##   ## ##   ##    P: AppCore-Runtime
5//         ## ##
6//                       C: unknown by dnettoRaw
7//    ##   ## ##   ##    U: working-tree by dnettoRaw
8//      ###########      S: 1.0.2-rc
9// =============================================================================
10
11//! Persist shareable events in terminal and bounded JSONL files.
12//!
13//! JSONL is for Safe or Diagnostic output only. Sensitive diagnostics use the
14//! encrypted DNT sink shown in the separate example.
15
16use appcore_log::{
17    FileArchiveConfig, FileSinkConfig, LogConfigError, LogError, LogOutputMode, LogPolicy,
18    LoggerConfig, Verbosity, LOG_SIZE_8_MIB,
19};
20use std::path::PathBuf;
21
22fn main() -> Result<(), appcore_log::LogConfigError> {
23    // Keep generated output predictable and inside Cargo's ignored target tree.
24    let output_directory = PathBuf::from("target/appcore-log-example");
25    std::fs::create_dir_all(&output_directory).map_err(|_| LogConfigError::Sink(LogError::Io))?;
26
27    let active_file = output_directory.join("application.jsonl");
28
29    // Two rotations stay beside the active file. Older rotations move into
30    // archive/YYYY/MM and the complete archive never exceeds 120 files.
31    let archive_directory = output_directory.join("archive");
32    let mut policy = LogPolicy::new(Verbosity::V4);
33    policy.set_component("sync", Verbosity::V8);
34
35    let logger = LoggerConfig {
36        policy,
37        output: LogOutputMode::TerminalAndFile,
38        file: Some(FileSinkConfig {
39            path: active_file,
40            max_bytes: LOG_SIZE_8_MIB,
41            sync_each_write: false,
42            retention: 2,
43            archive: Some(FileArchiveConfig {
44                directory: archive_directory,
45                max_files: 120,
46            }),
47        }),
48        ..LoggerConfig::default()
49    }
50    .build()?;
51
52    let application = logger.dispatcher().event(0, "application");
53    let sync = logger.dispatcher().event(1, "sync.transport");
54
55    application.info("application ready; inspect target/appcore-log-example/application.jsonl");
56
57    // The parent component policy makes this V7 diagnostic visible.
58    sync.verbosity(7).debug("replication batch sent");
59
60    sync.warn("peer response was delayed");
61
62    let stats = logger.dispatcher().stats();
63    assert_eq!(stats.sink_failures, 0);
64
65    Ok(())
66}