@@ -303,6 +303,9 @@ pub struct Logger {
303303 /// full paths, with `::` separaters (eg. before converting them from Rust to Python).
304304 filters : HashMap < String , LevelFilter > ,
305305
306+ /// The prefix to prepend to all log targets
307+ prefix : Option < String > ,
308+
306309 /// The imported Python `logging` module.
307310 logging : Py < PyModule > ,
308311
@@ -329,6 +332,7 @@ impl Logger {
329332 Ok ( Self {
330333 top_filter : LevelFilter :: Debug ,
331334 filters : HashMap :: new ( ) ,
335+ prefix : None ,
332336 logging : logging. into ( ) ,
333337 caching,
334338 cache : Default :: default ( ) ,
@@ -401,6 +405,15 @@ impl Logger {
401405 self
402406 }
403407
408+ /// Sets a prefix to prepend to log targets before sending log messages to Python.
409+ ///
410+ /// This allows for Python-side arrangements where logging configurations are only
411+ /// attached to logging names other than the root.
412+ pub fn set_prefix ( mut self , prefix : & str ) -> Self {
413+ self . prefix = Some ( prefix. replace ( "::" , "." ) ) ;
414+ self
415+ }
416+
404417 /// Finds a node in the cache.
405418 ///
406419 /// The hierarchy separator is `::`.
@@ -433,7 +446,11 @@ impl Logger {
433446 ) -> PyResult < Option < Py < PyAny > > > {
434447 let msg = format ! ( "{}" , record. args( ) ) ;
435448 let log_level = map_level ( record. level ( ) ) ;
436- let target = record. target ( ) . replace ( "::" , "." ) ;
449+ let mut target = record. target ( ) . replace ( "::" , "." ) ;
450+ target = match & self . prefix {
451+ Some ( prefix) => format ! ( "{}.{}" , prefix, target) ,
452+ None => target,
453+ } ;
437454 let cached_logger = cache
438455 . as_ref ( )
439456 . and_then ( |node| node. local . as_ref ( ) )
0 commit comments