Skip to content

Commit 5c848af

Browse files
cjdsellerspushkarm029
authored andcommitted
Add log filtering config options
Avoids the cost of forming Python strings for logs which will be ignored.
1 parent 3fa9de1 commit 5c848af

File tree

15 files changed

+71
-30
lines changed

15 files changed

+71
-30
lines changed

RELEASES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
Released on TBD (UTC).
44

55
### Enhancements
6-
None
6+
- Added `log_commands` config option for `ActorConfig`, `StrategyConfig`, `ExecAlgorithmConfig` for more efficient log filtering
77

88
### Breaking Changes
9-
None
9+
- Renamed `event_logging` config option to `log_events`
1010

1111
### Internal Improvements
1212
- Ported market order filling for OrderMatchingEngine in Rust (#2202), thanks @filipmacek
1313

1414
### Fixes
1515
- Fixed backtest start and end time validation assertion (#2203), thanks @davidsblom
1616
- Fixed `CustomData` import in `DataEngine` (#2207), thanks @graceyangfan and @faysou
17+
- Fixed databento helper function (#2208), thanks @faysou
1718

1819
### Documentation Updates
1920
None

nautilus_trader/common/actor.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ from nautilus_trader.portfolio.base cimport PortfolioFacade
5151

5252
cdef class Actor(Component):
5353
cdef object _executor
54+
cdef bint _log_events
55+
cdef bint _log_commands
5456
cdef set[type] _warning_events
5557
cdef dict[UUID4, object] _pending_requests
5658
cdef set[type] _pyo3_conversion_types

nautilus_trader/common/actor.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ cdef class Actor(Component):
130130
self._indicators_for_bars: dict[BarType, list[Indicator]] = {}
131131

132132
# Configuration
133+
self._log_events = config.log_events
134+
self._log_commands = config.log_commands
133135
self.config = config
134136

135137
self.trader_id = None # Initialized when registered
@@ -3290,7 +3292,7 @@ cdef class Actor(Component):
32903292
# -- EGRESS ---------------------------------------------------------------------------------------
32913293

32923294
cdef void _send_data_cmd(self, DataCommand command):
3293-
if is_logging_initialized():
3295+
if self._log_commands and is_logging_initialized():
32943296
self._log.info(f"{CMD}{SENT} {command}")
32953297
self._msgbus.send(endpoint="DataEngine.execute", msg=command)
32963298

nautilus_trader/common/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,17 @@ class ActorConfig(NautilusConfig, kw_only=True, frozen=True):
415415
component_id : ComponentId, optional
416416
The component ID. If ``None`` then the identifier will be taken from
417417
`type(self).__name__`.
418+
log_events : bool, default True
419+
If events should be logged by the actor.
420+
If False, then only warning events and above are logged.
421+
log_commands : bool, default True
422+
If commands should be logged by the actor.
418423
419424
"""
420425

421426
component_id: ComponentId | None = None
427+
log_events: bool = True
428+
log_commands: bool = True
422429

423430

424431
class ImportableActorConfig(NautilusConfig, frozen=True):

nautilus_trader/execution/algorithm.pyx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ from libc.stdint cimport uint64_t
2525
from nautilus_trader.cache.base cimport CacheFacade
2626
from nautilus_trader.common.actor cimport Actor
2727
from nautilus_trader.common.component cimport CMD
28-
from nautilus_trader.common.component cimport EVT
2928
from nautilus_trader.common.component cimport RECV
3029
from nautilus_trader.common.component cimport SENT
3130
from nautilus_trader.common.component cimport Clock
@@ -118,6 +117,8 @@ cdef class ExecAlgorithm(Actor):
118117
self.id = config.exec_algorithm_id or ExecAlgorithmId(type(self).__name__)
119118

120119
# Configuration
120+
self._log_events = config.log_events
121+
self._log_commands = config.log_commands
121122
self.config = config
122123

123124
self._exec_spawn_ids: dict[ClientOrderId, int] = {}
@@ -253,7 +254,8 @@ cdef class ExecAlgorithm(Actor):
253254
"""
254255
Condition.not_none(command, "command")
255256

256-
self._log.debug(f"{RECV}{CMD} {command}", LogColor.MAGENTA)
257+
if self._log_commands:
258+
self._log.debug(f"{RECV}{CMD} {command}", LogColor.MAGENTA)
257259

258260
if self._fsm.state != ComponentState.RUNNING:
259261
return
@@ -1460,16 +1462,16 @@ cdef class ExecAlgorithm(Actor):
14601462
# -- EGRESS ---------------------------------------------------------------------------------------
14611463

14621464
cdef void _send_emulator_command(self, TradingCommand command):
1463-
if is_logging_initialized():
1465+
if self._log_commands and is_logging_initialized():
14641466
self.log.info(f"{CMD}{SENT} {command}.")
14651467
self._msgbus.send(endpoint="OrderEmulator.execute", msg=command)
14661468

14671469
cdef void _send_risk_command(self, TradingCommand command):
1468-
if is_logging_initialized():
1470+
if self._log_commands and is_logging_initialized():
14691471
self.log.info(f"{CMD}{SENT} {command}.")
14701472
self._msgbus.send(endpoint="RiskEngine.execute", msg=command)
14711473

14721474
cdef void _send_exec_command(self, TradingCommand command):
1473-
if is_logging_initialized():
1475+
if self._log_commands and is_logging_initialized():
14741476
self.log.info(f"{CMD}{SENT} {command}.")
14751477
self._msgbus.send(endpoint="ExecEngine.execute", msg=command)

nautilus_trader/execution/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,17 @@ class ExecAlgorithmConfig(NautilusConfig, kw_only=True, frozen=True):
7171
exec_algorithm_id : ExecAlgorithmId, optional
7272
The unique ID for the execution algorithm.
7373
If not ``None`` then will become the execution algorithm ID.
74+
log_events : bool, default True
75+
If events should be logged by the execution algorithm.
76+
If False, then only warning events and above are logged.
77+
log_commands : bool, default True
78+
If commands should be logged by the execution algorithm.
7479
7580
"""
7681

7782
exec_algorithm_id: ExecAlgorithmId | None = None
83+
log_events: bool = True
84+
log_commands: bool = True
7885

7986

8087
class ImportableExecAlgorithmConfig(NautilusConfig, frozen=True):

nautilus_trader/execution/manager.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ cdef class OrderManager:
5151

5252
cdef readonly bint active_local
5353
cdef readonly bint debug
54+
cdef readonly bint log_events
55+
cdef readonly bint log_commands
5456

5557
cdef dict _submit_order_commands
5658
cdef object _submit_order_handler

nautilus_trader/execution/manager.pyx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ from libc.stdint cimport uint64_t
1919
from nautilus_trader.cache.cache cimport Cache
2020
from nautilus_trader.common.component cimport CMD
2121
from nautilus_trader.common.component cimport EVT
22-
from nautilus_trader.common.component cimport RECV
2322
from nautilus_trader.common.component cimport SENT
2423
from nautilus_trader.common.component cimport Clock
2524
from nautilus_trader.common.component cimport LogColor
@@ -101,6 +100,8 @@ cdef class OrderManager:
101100
cancel_order_handler: Callable[[Order], None] = None,
102101
modify_order_handler: Callable[[Order, Quantity], None] = None,
103102
bint debug = False,
103+
bint log_events = True,
104+
bint log_commands = True,
104105
):
105106
Condition.valid_string(component_name, "component_name")
106107
Condition.callable_or_none(submit_order_handler, "submit_order_handler")
@@ -114,6 +115,8 @@ cdef class OrderManager:
114115

115116
self.active_local = active_local
116117
self.debug = debug
118+
self.log_events = log_events
119+
self.log_commands = log_commands
117120
self._submit_order_handler = submit_order_handler
118121
self._cancel_order_handler = cancel_order_handler
119122
self._modify_order_handler = modify_order_handler
@@ -554,42 +557,42 @@ cdef class OrderManager:
554557
cpdef void send_emulator_command(self, TradingCommand command):
555558
Condition.not_none(command, "command")
556559

557-
if is_logging_initialized():
560+
if self.log_commands and is_logging_initialized():
558561
self._log.info(f"{CMD}{SENT} {command}") # pragma: no cover (no logging in tests)
559562
self._msgbus.send(endpoint="OrderEmulator.execute", msg=command)
560563

561564
cpdef void send_algo_command(self, TradingCommand command, ExecAlgorithmId exec_algorithm_id):
562565
Condition.not_none(command, "command")
563566
Condition.not_none(exec_algorithm_id, "exec_algorithm_id")
564567

565-
if is_logging_initialized():
568+
if self.log_commands and is_logging_initialized():
566569
self._log.info(f"{CMD}{SENT} {command}") # pragma: no cover (no logging in tests)
567570
self._msgbus.send(endpoint=f"{exec_algorithm_id}.execute", msg=command)
568571

569572
cpdef void send_risk_command(self, TradingCommand command):
570573
Condition.not_none(command, "command")
571574

572-
if is_logging_initialized():
575+
if self.log_commands and is_logging_initialized():
573576
self._log.info(f"{CMD}{SENT} {command}") # pragma: no cover (no logging in tests)
574577
self._msgbus.send(endpoint="RiskEngine.execute", msg=command)
575578

576579
cpdef void send_exec_command(self, TradingCommand command):
577580
Condition.not_none(command, "command")
578581

579-
if is_logging_initialized():
582+
if self.log_commands and is_logging_initialized():
580583
self._log.info(f"{CMD}{SENT} {command}") # pragma: no cover (no logging in tests)
581584
self._msgbus.send(endpoint="ExecEngine.execute", msg=command)
582585

583586
cpdef void send_risk_event(self, OrderEvent event):
584587
Condition.not_none(event, "event")
585588

586-
if is_logging_initialized():
589+
if self.log_events and is_logging_initialized():
587590
self._log.info(f"{EVT}{SENT} {event}") # pragma: no cover (no logging in tests)
588591
self._msgbus.send(endpoint="RiskEngine.process", msg=event)
589592

590593
cpdef void send_exec_event(self, OrderEvent event):
591594
Condition.not_none(event, "event")
592595

593-
if is_logging_initialized():
596+
if self.log_events and is_logging_initialized():
594597
self._log.info(f"{EVT}{SENT} {event}") # pragma: no cover (no logging in tests)
595598
self._msgbus.send(endpoint="ExecEngine.process", msg=event)

nautilus_trader/trading/config.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ class StrategyConfig(NautilusConfig, kw_only=True, frozen=True):
5151
manage_gtd_expiry : bool, default False
5252
If all order GTD time in force expirations should be managed by the strategy.
5353
If True, then will ensure open orders have their GTD timers re-activated on start.
54-
event_logging : bool, default True
55-
If event logging should be enabled for the strategy.
54+
log_events : bool, default True
55+
If events should be logged by the strategy.
5656
If False, then only warning events and above are logged.
57+
log_commands : bool, default True
58+
If commands should be logged by the strategy.
5759
5860
"""
5961

@@ -63,7 +65,8 @@ class StrategyConfig(NautilusConfig, kw_only=True, frozen=True):
6365
external_order_claims: list[InstrumentId] | None = None
6466
manage_contingent_orders: bool = False
6567
manage_gtd_expiry: bool = False
66-
event_logging: bool = True
68+
log_events: bool = True
69+
log_commands: bool = True
6770

6871

6972
class ImportableStrategyConfig(NautilusConfig, frozen=True):

nautilus_trader/trading/strategy.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ cdef class Strategy(Actor):
8181
"""If contingent orders should be managed automatically by the strategy.\n\n:returns: `bool`"""
8282
cdef readonly bint manage_gtd_expiry
8383
"""If all order GTD time in force expirations should be managed automatically by the strategy.\n\n:returns: `bool`"""
84-
cdef readonly bint event_logging
85-
"""If event logging should be enabled for the strategy.\n\n:returns: `bool`"""
8684

8785
# -- REGISTRATION ---------------------------------------------------------------------------------
8886

0 commit comments

Comments
 (0)