witchcraft_log/
lib.rs

1// Copyright 2019 Palantir Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//! A structured logging facade for Witchcraft servers.
15//!
16//! `witchcraft-log` is structured quite similarly to the standard Rust `log` crate. Its usage in libraries versus
17//! executables, log levels, etc are all mostly identical. However, `witchcraft-log` does differ from `log` in some
18//! key ways.
19//!
20//! # Structured Logging
21//!
22//! Witchcraft logs are *structured*. Rather than including runtime information by interpolating them into the log
23//! message, information is included via a separate set of parameters. Parameters are partitioned into "safe" parameters
24//! and "unsafe" parameters. Safety in this context is *not* safety in the traditional Rust sense of memory safety, but
25//! instead safety against information leakage. Safe parameters do not contain any sensitive information about use of a
26//! service and can be exfiltrated from a specific environment, while unsafe parameters contain sensitive information
27//! that should not leave the environment at all. For example, the amount of memory used to process a request could be
28//! a safe parameter, while information about the user executing the request could be an unsafe parameter.
29//!
30//! Parameters can be arbitrary `serde`-serializable values. Note, however, that loggers may commonly serialize
31//! parameters to JSON, so values that cannot be serialized into JSON are not recommended.
32//!
33//! All dynamic information in the log record should be represented via parameters. In fact, Witchcraft-log requires the
34//! log message to be a static string - no interpolation of any kind can be performed. This means that the message
35//! itself can always be considered safe.
36//!
37//! ## Examples
38//!
39//! ```
40//! # let (user_id, memory_overhead) = ("", "");
41//! // with the standard log crate
42//! log::info!("ran a request for {} using {} bytes of memory", user_id, memory_overhead);
43//!
44//! // with the witchcraft-log crate
45//! witchcraft_log::info!("ran a request", safe: { memory: memory_overhead }, unsafe: { user: user_id });
46//! ```
47//!
48//! # Errors
49//!
50//! Additionally, a `conjure_error::Error` can be associated with a log message. Since many logs occur due to an error,
51//! this allows more information about the error (e.g. its stacktrace) to be automatically included in the record.
52//!
53//! ## Examples
54//!
55//! ```
56//! # fn shave_a_yak(_: ()) -> Result<(), conjure_error::Error> { Ok(()) }
57//! # let my_yak = ();
58//! if let Err(e) = shave_a_yak(my_yak) {
59//!     witchcraft_log::warn!("error shaving a yak", safe: { yak: my_yak }, error: e);
60//! }
61//! ```
62//!
63//! # Bridging
64//!
65//! Even when an application is using `witchcraft-log`, many of its dependencies may still use the `log` crate. The
66//! `bridge` module provides functionality to forward records from the `log` crate to `witchcraft-log`.
67#![warn(missing_docs)]
68
69pub use crate::level::*;
70pub use crate::logger::*;
71pub use crate::record::*;
72
73pub mod bridge;
74mod level;
75mod logger;
76#[macro_use]
77mod macros;
78pub mod mdc;
79#[doc(hidden)]
80pub mod private;
81mod record;
82
83#[cfg(test)]
84mod test;