agent: improve logger implemetation

Improve loger implementation by:
1) avoid unnecessary clone() operations.
2) change Arc<Mutex<slog::Level>> to Mutex<slog::Level>. We should use
atomic<usize> instead of Mutex<slog::Level> for better performance here.
But with slog_async::Async drainer in the pipeline, RuntimeLevelFilter
drainer will only get from a single-thread context, so keep it as is.
3) minor syntax cleanups.

Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
This commit is contained in:
Liu Jiang 2019-11-30 18:33:07 +08:00
parent b6c8b9d9f4
commit eb6258b751

View File

@ -9,7 +9,7 @@ use std::io;
use std::io::Write; use std::io::Write;
use std::process; use std::process;
use std::result; use std::result;
use std::sync::{Arc, Mutex}; use std::sync::Mutex;
// XXX: 'writer' param used to make testing possible. // XXX: 'writer' param used to make testing possible.
pub fn create_logger<W>(name: &str, source: &str, level: slog::Level, writer: W) -> slog::Logger pub fn create_logger<W>(name: &str, source: &str, level: slog::Level, writer: W) -> slog::Logger
@ -41,16 +41,6 @@ where
) )
} }
impl KV for HashSerializer {
fn serialize(&self, _record: &Record, serializer: &mut dyn slog::Serializer) -> slog::Result {
for (key, value) in self.fields.clone().into_iter() {
serializer.emit_str(Key::from(key), &value)?;
}
Ok(())
}
}
// Used to convert an slog::OwnedKVList into a hash map. // Used to convert an slog::OwnedKVList into a hash map.
struct HashSerializer { struct HashSerializer {
fields: HashMap<String, String>, fields: HashMap<String, String>,
@ -77,6 +67,16 @@ impl HashSerializer {
} }
} }
impl KV for HashSerializer {
fn serialize(&self, _record: &Record, serializer: &mut dyn slog::Serializer) -> slog::Result {
for (key, value) in self.fields.iter() {
serializer.emit_str(Key::from(key.to_string()), value)?;
}
Ok(())
}
}
impl slog::Serializer for HashSerializer { impl slog::Serializer for HashSerializer {
fn emit_arguments(&mut self, key: Key, value: &std::fmt::Arguments) -> slog::Result { fn emit_arguments(&mut self, key: Key, value: &std::fmt::Arguments) -> slog::Result {
self.add_field(format!("{}", key), format!("{}", value)); self.add_field(format!("{}", key), format!("{}", value));
@ -90,13 +90,13 @@ struct UniqueDrain<D> {
impl<D> UniqueDrain<D> { impl<D> UniqueDrain<D> {
fn new(drain: D) -> Self { fn new(drain: D) -> Self {
UniqueDrain { drain: drain } UniqueDrain { drain }
} }
} }
impl<D> Drain for UniqueDrain<D> impl<D> Drain for UniqueDrain<D>
where where
D: slog::Drain, D: Drain,
{ {
type Ok = (); type Ok = ();
type Err = io::Error; type Err = io::Error;
@ -136,21 +136,19 @@ where
// specified in the struct. // specified in the struct.
struct RuntimeLevelFilter<D> { struct RuntimeLevelFilter<D> {
drain: D, drain: D,
level: Arc<Mutex<slog::Level>>, level: Mutex<slog::Level>,
} }
impl<D> RuntimeLevelFilter<D> { impl<D> RuntimeLevelFilter<D> {
fn new(drain: D, level: slog::Level) -> Self { fn new(drain: D, level: slog::Level) -> Self {
RuntimeLevelFilter { RuntimeLevelFilter {
drain: drain, drain,
level: Arc::new(Mutex::new(level)), level: Mutex::new(level),
} }
} }
fn set_level(&self, level: slog::Level) { fn set_level(&self, level: slog::Level) {
let level_ref = self.level.clone(); let mut log_level = self.level.lock().unwrap();
let mut log_level = level_ref.lock().unwrap();
*log_level = level; *log_level = level;
} }
@ -168,9 +166,7 @@ where
record: &slog::Record, record: &slog::Record,
values: &slog::OwnedKVList, values: &slog::OwnedKVList,
) -> result::Result<Self::Ok, Self::Err> { ) -> result::Result<Self::Ok, Self::Err> {
let level_ref = self.level.clone(); let log_level = self.level.lock().unwrap();
let log_level = level_ref.lock().unwrap();
if record.level().is_at_least(*log_level) { if record.level().is_at_least(*log_level) {
self.drain.log(record, values)?; self.drain.log(record, values)?;