Skip to content

Commit faa87a9

Browse files
authored
Initalize logger with formatting
1 parent 23b6e79 commit faa87a9

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/fastly/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
chrono = "0.4.41"
78
error-stack = "0.5"
89
fastly = "0.11.5"
10+
fern = "0.7.1"
911
futures = "0.3"
1012
log = "0.4.20"
1113
log-fastly = "0.11.5"

crates/fastly/src/main.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use fastly::http::{header, Method, StatusCode};
22
use fastly::{Error, Request, Response};
3-
use log::LevelFilter::Info;
3+
use log_fastly::Logger;
44

55
mod error;
66
use crate::error::to_error_response;
@@ -20,7 +20,7 @@ use trusted_server_common::why::handle_why_trusted_server;
2020

2121
#[fastly::main]
2222
fn main(req: Request) -> Result<Response, Error> {
23-
log_fastly::init_simple("mylogs", Info);
23+
init_logger();
2424

2525
let settings = match Settings::new() {
2626
Ok(s) => s,
@@ -83,3 +83,25 @@ fn not_found_response() -> Response {
8383
.with_header(header::CONTENT_TYPE, "text/plain")
8484
.with_header(HEADER_X_COMPRESS_HINT, "on")
8585
}
86+
87+
fn init_logger() {
88+
let logger = Logger::builder()
89+
.default_endpoint("tslog")
90+
.echo_stdout(true)
91+
.max_level(log::LevelFilter::Debug)
92+
.build()
93+
.expect("Failed to build Logger");
94+
95+
fern::Dispatch::new()
96+
.format(|out, message, record| {
97+
out.finish(format_args!(
98+
"{} {} {}",
99+
chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
100+
record.level(),
101+
message
102+
))
103+
})
104+
.chain(Box::new(logger) as Box<dyn log::Log>)
105+
.apply()
106+
.expect("Failed to initialize logger");
107+
}

0 commit comments

Comments
 (0)