Skip to content

Commit 8fb7a7c

Browse files
committed
fix: move the logging message where it's effective
Move the message after the logger has been configured, so it actually gets printed. Eliminate redundant message that outputs internal vmm crate version 0.1.0. Fixes: 96ed457, 332f218 (cherry picked from commit 78c26af) Signed-off-by: Pablo Barbáchano <[email protected]>
1 parent 3edba6d commit 8fb7a7c

File tree

4 files changed

+52
-100
lines changed

4 files changed

+52
-100
lines changed

src/firecracker/src/main.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,32 @@ fn main_exec() -> Result<(), MainError> {
264264
return Ok(());
265265
}
266266

267+
// It's safe to unwrap here because the field's been provided with a default value.
268+
let instance_id = arguments.single_value("id").unwrap();
269+
validate_instance_id(instance_id.as_str()).expect("Invalid instance ID");
270+
271+
// Apply the logger configuration.
272+
vmm::logger::INSTANCE_ID
273+
.set(String::from(instance_id))
274+
.unwrap();
275+
let log_path = arguments.single_value("log-path").map(PathBuf::from);
276+
let level = arguments
277+
.single_value("level")
278+
.map(|s| vmm::logger::LevelFilter::from_str(s))
279+
.transpose()
280+
.map_err(MainError::InvalidLogLevel)?;
281+
let show_level = arguments.flag_present("show-level").then_some(true);
282+
let show_log_origin = arguments.flag_present("show-level").then_some(true);
283+
LOGGER
284+
.update(LoggerConfig {
285+
log_path,
286+
level,
287+
show_level,
288+
show_log_origin,
289+
})
290+
.map_err(MainError::LoggerInitialization)?;
291+
info!("Running Firecracker v{FIRECRACKER_VERSION}");
292+
267293
register_signal_handlers().map_err(MainError::RegisterSignalHandlers)?;
268294

269295
#[cfg(target_arch = "aarch64")]
@@ -287,39 +313,13 @@ fn main_exec() -> Result<(), MainError> {
287313
// deprecating one.
288314
// warn_deprecated_parameters(&arguments);
289315

290-
// It's safe to unwrap here because the field's been provided with a default value.
291-
let instance_id = arguments.single_value("id").unwrap();
292-
validate_instance_id(instance_id.as_str()).expect("Invalid instance ID");
293-
294316
let instance_info = InstanceInfo {
295317
id: instance_id.clone(),
296318
state: VmState::NotStarted,
297319
vmm_version: FIRECRACKER_VERSION.to_string(),
298320
app_name: "Firecracker".to_string(),
299321
};
300322

301-
let id = arguments.single_value("id").map(|s| s.as_str()).unwrap();
302-
vmm::logger::INSTANCE_ID.set(String::from(id)).unwrap();
303-
info!("Running Firecracker v{FIRECRACKER_VERSION}");
304-
305-
// Apply the logger configuration.
306-
let log_path = arguments.single_value("log-path").map(PathBuf::from);
307-
let level = arguments
308-
.single_value("level")
309-
.map(|s| vmm::logger::LevelFilter::from_str(s))
310-
.transpose()
311-
.map_err(MainError::InvalidLogLevel)?;
312-
let show_level = arguments.flag_present("show-level").then_some(true);
313-
let show_log_origin = arguments.flag_present("show-level").then_some(true);
314-
LOGGER
315-
.update(LoggerConfig {
316-
log_path,
317-
level,
318-
show_level,
319-
show_log_origin,
320-
})
321-
.map_err(MainError::LoggerInitialization)?;
322-
323323
if let Some(metrics_path) = arguments.single_value("metrics-path") {
324324
let metrics_config = MetricsConfig {
325325
metrics_path: PathBuf::from(metrics_path),

src/vmm/src/logger/logging.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ pub const DEFAULT_LEVEL: log::LevelFilter = log::LevelFilter::Info;
2222
pub const DEFAULT_INSTANCE_ID: &str = "anonymous-instance";
2323
/// Instance id.
2424
pub static INSTANCE_ID: OnceLock<String> = OnceLock::new();
25-
/// Semver version of Firecracker.
26-
const FIRECRACKER_VERSION: &str = env!("CARGO_PKG_VERSION");
2725

2826
/// The logger.
2927
///
@@ -62,7 +60,7 @@ impl Logger {
6260
.unwrap_or(DEFAULT_LEVEL),
6361
);
6462

65-
let target_changed = if let Some(log_path) = config.log_path {
63+
if let Some(log_path) = config.log_path {
6664
let file = std::fs::OpenOptions::new()
6765
.custom_flags(libc::O_NONBLOCK)
6866
.read(true)
@@ -71,9 +69,6 @@ impl Logger {
7169
.map_err(LoggerUpdateError)?;
7270

7371
guard.target = Some(file);
74-
true
75-
} else {
76-
false
7772
};
7873

7974
if let Some(show_level) = config.show_level {
@@ -87,11 +82,6 @@ impl Logger {
8782
// Ensure we drop the guard before attempting to log, otherwise this
8883
// would deadlock.
8984
drop(guard);
90-
if target_changed {
91-
// We log this when a target is changed so it is always the 1st line logged, this
92-
// maintains some previous behavior to minimize a breaking change.
93-
log::info!("Running Firecracker v{FIRECRACKER_VERSION}");
94-
}
9585

9686
Ok(())
9787
}

tests/framework/microvm.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ def spawn(
450450
self,
451451
log_file="fc.log",
452452
log_level="Debug",
453+
log_show_level=False,
454+
log_show_origin=False,
453455
metrics_path="fc.ndjson",
454456
):
455457
"""Start a microVM as a daemon or in a screen session."""
@@ -461,10 +463,14 @@ def spawn(
461463
self.log_file = Path(self.path) / log_file
462464
self.log_file.touch()
463465
self.create_jailed_resource(self.log_file)
464-
# The default value for `level`, when configuring the
465-
# logger via cmd line, is `Warning`. We set the level
466-
# to `Debug` to also have the boot time printed in fifo.
466+
# The default value for `level`, when configuring the logger via cmd
467+
# line, is `Info`. We set the level to `Debug` to also have the boot
468+
# time printed in the log.
467469
self.jailer.extra_args.update({"log-path": log_file, "level": log_level})
470+
if log_show_level:
471+
self.jailer.extra_args["show-level"] = None
472+
if log_show_origin:
473+
self.jailer.extra_args["show-log-origin"] = None
468474

469475
if metrics_path is not None:
470476
self.metrics_file = Path(self.path) / metrics_path

tests/integration_tests/functional/test_logging.py

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""Tests the format of human readable logs.
44
55
It checks the response of the API configuration calls and the logs that show
6-
up in the configured logging FIFO.
6+
up in the configured logging file.
77
"""
88

99
import re
@@ -71,48 +71,6 @@ def check_log_message_format(log_str, instance_id, level, show_level, show_origi
7171
assert tag_level_no <= configured_level_no
7272

7373

74-
def test_no_origin_logs(test_microvm_with_api):
75-
"""
76-
Check that logs do not contain the origin (i.e file and line number).
77-
"""
78-
_test_log_config(microvm=test_microvm_with_api, show_level=True, show_origin=False)
79-
80-
81-
def test_no_level_logs(test_microvm_with_api):
82-
"""
83-
Check that logs do not contain the level.
84-
"""
85-
_test_log_config(microvm=test_microvm_with_api, show_level=False, show_origin=True)
86-
87-
88-
def test_no_nada_logs(test_microvm_with_api):
89-
"""
90-
Check that logs do not contain either level or origin.
91-
"""
92-
_test_log_config(microvm=test_microvm_with_api, show_level=False, show_origin=False)
93-
94-
95-
def test_info_logs(test_microvm_with_api):
96-
"""
97-
Check output of logs when minimum level to be displayed is info.
98-
"""
99-
_test_log_config(microvm=test_microvm_with_api)
100-
101-
102-
def test_warn_logs(test_microvm_with_api):
103-
"""
104-
Check output of logs when minimum level to be displayed is warning.
105-
"""
106-
_test_log_config(microvm=test_microvm_with_api, log_level="Warning")
107-
108-
109-
def test_error_logs(test_microvm_with_api):
110-
"""
111-
Check output of logs when minimum level of logs displayed is error.
112-
"""
113-
_test_log_config(microvm=test_microvm_with_api, log_level="Error")
114-
115-
11674
def test_log_config_failure(test_microvm_with_api):
11775
"""
11876
Check passing invalid FIFOs is detected and reported as an error.
@@ -212,27 +170,25 @@ def test_api_requests_logs(test_microvm_with_api):
212170
)
213171

214172

215-
# pylint: disable=W0102
216-
def _test_log_config(microvm, log_level="Info", show_level=True, show_origin=True):
173+
@pytest.mark.parametrize(
174+
"log_level,show_level,show_origin",
175+
[
176+
("Info", True, True),
177+
("Info", False, True),
178+
("Info", True, False),
179+
("Info", False, False),
180+
("Error", False, False),
181+
("Warning", False, False),
182+
],
183+
)
184+
def test_log_config(uvm_plain, log_level, show_level, show_origin):
217185
"""Exercises different scenarios for testing the logging config."""
218-
microvm.spawn(log_file=None)
219-
# only works if log level is Debug
220-
microvm.time_api_requests = False
221-
222-
# Configure logging.
223-
log_path = Path(microvm.path) / "log"
224-
log_path.touch()
225-
microvm.api.logger.put(
226-
log_path=microvm.create_jailed_resource(log_path),
227-
level=log_level,
228-
show_level=show_level,
229-
show_log_origin=show_origin,
186+
microvm = uvm_plain
187+
microvm.spawn(
188+
log_level=log_level, log_show_level=show_level, log_show_origin=show_origin
230189
)
231-
microvm.log_file = log_path
232-
233190
microvm.basic_config()
234191
microvm.start()
235-
236192
lines = microvm.log_data.splitlines()
237193

238194
# Check for `Running Firecracker` message.

0 commit comments

Comments
 (0)