Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move container-hotplug from wrapper of docker to wrapper of runc #6

Merged
merged 7 commits into from
Apr 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add runc-compatible json logger
nbdd0121 committed Apr 23, 2024
commit 76e0cd0220765e9a3b324037aca331128abd130e
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -22,6 +22,10 @@ bollard = "0.16"
rustix = { version = "0.38", features = ["fs", "stdio", "termios", "process", "thread"] }
bitflags = "2"
once_cell = "1"
humantime = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

aya = { git = "https://github.com/aya-rs/aya.git" }

[build-dependencies]
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ mod cli;
mod dev;
mod docker;
mod hotplug;
mod runc;
mod util;

use cli::Action;
56 changes: 56 additions & 0 deletions src/runc/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::io::{BufWriter, Write};
use std::sync::Mutex;
use std::time::SystemTime;

use log::{Level, Log};
use serde::Serialize;

#[derive(Serialize)]
struct Message {
level: &'static str,
msg: String,
time: String,
}

fn map_log_level(level: Level) -> &'static str {
match level {
Level::Error => "error",
Level::Warn => "warning",
Level::Info => "info",
Level::Debug => "debug",
Level::Trace => "trace",
}
}

/// JSON logger compatible with logrus (the logging library used by runc and many go projects).
pub struct JsonLogger {
target: Mutex<BufWriter<Box<dyn Write + Send>>>,
}

impl JsonLogger {
pub fn new(target: Box<dyn Write + Send>) -> Self {
Self {
target: Mutex::new(BufWriter::new(target)),
}
}
}

impl Log for JsonLogger {
fn enabled(&self, _: &log::Metadata) -> bool {
true
}

fn log(&self, record: &log::Record) {
let msg = Message {
level: map_log_level(record.level()),
msg: record.args().to_string(),
time: humantime::format_rfc3339_seconds(SystemTime::now()).to_string(),
};

let mut target = self.target.lock().unwrap();
let _ = serde_json::to_writer(&mut *target, &msg);
let _ = target.flush();
}

fn flush(&self) {}
}
1 change: 1 addition & 0 deletions src/runc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod log;