Skip to content

Commit

Permalink
Fix file writer log file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Feb 9, 2024
1 parent 22e5122 commit d9511b7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 3 additions & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NautilusTrader 1.187.0 Beta

Released on TBD (UTC).
Released on 9th February 2024 (UTC).

### Enhancements
- Refined logging system module and writers in Rust, thanks @ayush-sb and @twitu
Expand All @@ -11,6 +11,8 @@ Released on TBD (UTC).

### Fixes
- Fixed `BacktestEngine` and `Trader` disposal (now properly releasing resources), thanks for reporting @davidsblom
- Fixed circular import issues from configuration objects, thanks for reporting @cuberone
- Fixed unnecessary creation of log files when file logging off

---

Expand Down
2 changes: 1 addition & 1 deletion examples/live/binance/binance_spot_market_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
logging=LoggingConfig(
log_level="INFO",
# log_level_file="DEBUG",
# log_colors=False,
# log_file_format="json",
),
exec_engine=LiveExecEngineConfig(
reconciliation=True,
Expand Down
16 changes: 10 additions & 6 deletions nautilus_core/common/src/logging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl LogLineWrapper {
}
}

pub fn get_string(&mut self) -> &String {
pub fn get_string(&mut self) -> &str {
self.cache.get_or_insert_with(|| {
format!(
"{} [{}] {}.{}: {}\n",
Expand All @@ -331,7 +331,7 @@ impl LogLineWrapper {
})
}

pub fn get_colored(&mut self) -> &String {
pub fn get_colored(&mut self) -> &str {
self.colored.get_or_insert_with(|| {
format!(
"\x1b[1m{}\x1b[0m {}[{}] {}.{}: {}\x1b[0m\n",
Expand Down Expand Up @@ -366,7 +366,6 @@ impl Log for Logger {
}

fn log(&self, record: &log::Record) {
// TODO remove unwraps
if self.enabled(record.metadata()) {
let key_values = record.key_values();
let color = key_values
Expand Down Expand Up @@ -469,8 +468,13 @@ impl Logger {
// Setup std I/O buffers
let mut stdout_writer = StdoutWriter::new(stdout_level, is_colored);
let mut stderr_writer = StderrWriter::new(is_colored);
let mut file_writer =
FileWriter::new(trader_id.clone(), instance_id, file_config, fileout_level);

// Conditionally create file writer based on fileout_level
let mut file_writer_opt = if fileout_level != LevelFilter::Off {
FileWriter::new(trader_id.clone(), instance_id, file_config, fileout_level)
} else {
None
};

// Continue to receive and handle log events until channel is hung up
while let Ok(event) = rx.recv() {
Expand Down Expand Up @@ -515,7 +519,7 @@ impl Logger {
stdout_writer.flush();
}

if let Some(ref mut writer) = file_writer {
if let Some(ref mut writer) = file_writer_opt {
if writer.enabled(&wrapper.line) {
if writer.json_format {
writer.write(&wrapper.get_json());
Expand Down

0 comments on commit d9511b7

Please sign in to comment.