1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
// Copyright Sebastian Wiesner <sebastian@swsnr.de>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Test that we actually log to the systemd journal,
//! and that systemd can pick all those messages up.log_
#![deny(warnings, clippy::all)]
use log::kv::Value;
use log::{Level, Log, Record};
use similar_asserts::assert_eq;
use systemd_journal_logger::JournalLog;
mod journal;
#[test]
fn simple_log_entry() {
JournalLog::new().unwrap().log(
&Record::builder()
.level(Level::Warn)
.target("simple_log_entry")
.module_path(Some(module_path!()))
.file(Some(file!()))
.line(Some(92749))
.args(format_args!("systemd_journal_logger test: {}", 42))
.build(),
);
let entry = journal::read_one_entry("simple_log_entry");
assert_eq!(entry["TARGET"], "simple_log_entry");
assert_eq!(entry["PRIORITY"], "4");
assert_eq!(entry["MESSAGE"], "systemd_journal_logger test: 42");
assert_eq!(entry["CODE_FILE"], file!());
assert_eq!(entry["CODE_LINE"], "92749");
assert_eq!(entry["CODE_MODULE"], module_path!());
assert!(entry["SYSLOG_IDENTIFIER"]
.as_text()
.contains("log_to_journal"));
assert_eq!(
entry["SYSLOG_IDENTIFIER"],
std::env::current_exe()
.unwrap()
.file_name()
.unwrap()
.to_str()
.unwrap()
);
assert_eq!(entry["SYSLOG_PID"], std::process::id().to_string());
// // The PID we logged is equal to the PID systemd determined as source for our process
assert_eq!(entry["SYSLOG_PID"], entry["_PID"]);
}
#[test]
fn internal_null_byte_in_message() {
JournalLog::new().unwrap().log(
&Record::builder()
.level(Level::Warn)
.target("internal_null_byte_in_message")
.args(format_args!("systemd_journal_logger with \x00 byte"))
.build(),
);
let entry = journal::read_one_entry("internal_null_byte_in_message");
assert_eq!(entry["PRIORITY"], "4");
assert_eq!(
entry["MESSAGE"].as_text(),
"systemd_journal_logger with \x00 byte"
);
}
#[test]
fn multiline_message() {
JournalLog::new().unwrap().log(
&Record::builder()
.level(Level::Error)
.target("multiline_message")
.args(format_args!(
"systemd_journal_logger test\nwith\nline {}",
"breaks"
))
.build(),
);
let entry = journal::read_one_entry("multiline_message");
assert_eq!(entry["PRIORITY"], "3");
assert_eq!(
entry["MESSAGE"],
"systemd_journal_logger test\nwith\nline breaks"
);
}
#[test]
fn trailing_newline_message() {
JournalLog::new().unwrap().log(
&Record::builder()
.level(Level::Trace)
.target("trailing_newline_message")
.args(format_args!("trailing newline\n"))
.build(),
);
let entry = journal::read_one_entry("trailing_newline_message");
assert_eq!(entry["PRIORITY"], "7");
assert_eq!(entry["MESSAGE"], "trailing newline\n");
}
#[test]
fn very_large_message() {
let very_large_string = "b".repeat(512_000);
JournalLog::new().unwrap().log(
&Record::builder()
.level(Level::Trace)
.target("very_large_message")
.args(format_args!("{}", very_large_string))
.build(),
);
let entry = journal::read_one_entry("very_large_message");
assert_eq!(entry["PRIORITY"], "7");
assert_eq!(entry["MESSAGE"].as_text(), very_large_string);
}
#[test]
fn extra_fields() {
JournalLog::new()
.unwrap()
.with_extra_fields(vec![("FOO", "BAR")])
.log(
&Record::builder()
.level(Level::Debug)
.target("extra_fields")
.args(format_args!("with an extra field"))
.build(),
);
let entry = journal::read_one_entry("extra_fields");
assert_eq!(entry["PRIORITY"], "6");
assert_eq!(entry["MESSAGE"], "with an extra field");
assert_eq!(entry["FOO"], "BAR")
}
#[test]
fn escaped_extra_fields() {
JournalLog::new()
.unwrap()
.with_extra_fields(vec![
("Hallöchen", "Welt"),
("123_FOO", "BAR"),
("_spam", "EGGS"),
])
.log(
&Record::builder()
.level(Level::Debug)
.target("escaped_extra_fields")
.args(format_args!("with an escaped extra field"))
.build(),
);
let entry = journal::read_one_entry("escaped_extra_fields");
assert_eq!(entry["PRIORITY"], "6");
assert_eq!(entry["MESSAGE"], "with an escaped extra field");
assert_eq!(entry["HALL_CHEN"], "Welt");
assert_eq!(entry["ESCAPED_123_FOO"], "BAR");
assert_eq!(entry["ESCAPED__SPAM"], "EGGS");
}
#[test]
fn extra_record_fields() {
let kvs: &[(&str, Value)] = &[
("_foo", Value::from("foo")),
("spam_with_eggs", Value::from(false)),
];
JournalLog::new()
.unwrap()
.with_extra_fields(vec![("EXTRA_FIELD", "foo")])
.log(
&Record::builder()
.level(Level::Error)
.target("extra_record_fields")
.args(format_args!("Hello world"))
.key_values(&kvs)
.build(),
);
let entry = journal::read_one_entry("extra_record_fields");
assert_eq!(entry["PRIORITY"], "3");
assert_eq!(entry["MESSAGE"], "Hello world");
assert_eq!(entry["EXTRA_FIELD"], "foo");
assert_eq!(entry["ESCAPED__FOO"], "foo");
assert_eq!(entry["SPAM_WITH_EGGS"], "false");
}
#[test]
fn duplicate_fields() {
let kvs: &[(&str, Value)] = &[("FOO", Value::from("record foo"))];
JournalLog::new()
.unwrap()
.with_extra_fields(vec![("FOO", "logger foo")])
.log(
&Record::builder()
.level(Level::Error)
.target("duplicate_fields")
.args(format_args!("Hello world"))
.key_values(&kvs)
.build(),
);
let entry = journal::read_one_entry("duplicate_fields");
assert_eq!(entry["PRIORITY"], "3");
assert_eq!(entry["MESSAGE"], "Hello world");
// First the field value from the record, then the one from the logger itself,
// since we append extra fields of the logger at the very end.
assert_eq!(entry["FOO"], vec!["record foo", "logger foo"]);
}
|