6161# Store a formatter for use with the main log
6262_main_log_formatter = Formatter ('- %(name)s\t -\t %(levelname)s\n \t %(message)s' )
6363
64+ # SP's logs prefix
65+ SP_LOG_PREFIX = '[Source.Python] '
66+
6467
6568# =============================================================================
6669# >> CLASSES
6770# =============================================================================
6871class _LogInstance (dict ):
6972 """Base logging class used to create child logging instances."""
7073
71- def __init__ (self , parent = None , name = None ):
74+ def __init__ (self , parent = None , name = None , prefix = None ):
7275 """Store the parent and gets a child of the parent.
7376
7477 :param _LogInstance parent:
@@ -88,13 +91,16 @@ def __init__(self, parent=None, name=None):
8891 # Store a child logging instance
8992 self ._logger = self .parent .logger .getChild (name )
9093
94+ # Store the given prefix
95+ self .prefix = prefix
96+
9197 def __missing__ (self , item ):
9298 """Add new items as logging instances.
9399
94100 :rtype: _LogInstance
95101 """
96102 # Get the new logging instance
97- value = self [item ] = _LogInstance (self , item )
103+ value = self [item ] = _LogInstance (self , item , prefix = self . prefix )
98104
99105 # Return the logging instance
100106 return value
@@ -242,6 +248,11 @@ def _log(self, level, msg, *args, **kwargs):
242248 # Get the message to send
243249 message = _main_log_formatter .format (record )
244250
251+ # Prepend prefix
252+ prefix = self .prefix
253+ if prefix is not None :
254+ message = prefix + message
255+
245256 # Print to the main log
246257 engine_server .log_print (message + '\n ' )
247258
@@ -252,6 +263,12 @@ def _log(self, level, msg, *args, **kwargs):
252263 # If <engine>.log_print is called with logging being on,
253264 # the console is already echoed with the message.
254265 from core import echo_console
266+
267+ # Prepend prefix
268+ prefix = self .prefix
269+ if prefix is not None :
270+ msg = prefix + msg
271+
255272 echo_console (msg )
256273
257274 # Print to the script's log file?
@@ -336,7 +353,8 @@ class LogManager(AutoUnload, _LogInstance):
336353
337354 def __init__ (
338355 self , name , level , areas , filepath = None ,
339- log_format = None , date_format = None , encoding = 'utf-8' ):
356+ log_format = None , date_format = None , encoding = 'utf-8' ,
357+ prefix = None ):
340358 """Store the base values and creates the logger.
341359
342360 :param str name:
@@ -354,7 +372,7 @@ def __init__(
354372 A custom date format that defines how the date is printed.
355373 """
356374 # Initialize the dictionary
357- super ().__init__ ()
375+ super ().__init__ (prefix = prefix )
358376
359377 # Store the base formatter
360378 self ._formatter = Formatter (log_format , date_format )
@@ -451,7 +469,8 @@ def getFilesToDelete(self):
451469 'sp' , _level , _areas ,
452470 'source-python.{0}' .format (date .today ().strftime ('%Y-%m-%d' )),
453471 '%(asctime)s - %(logger_name)s\t -\t %(levelname)s\t %(message)s' ,
454- '%Y-%m-%d %H:%M:%S' )
472+ '%Y-%m-%d %H:%M:%S' ,
473+ prefix = SP_LOG_PREFIX )
455474
456475# Set the parent logger level to allow all message types
457476_sp_logger .logger .parent .level = DEBUG
0 commit comments