Design Pattern Examples
Overview of object-oriented design patterns
bridge_logger.py
Go to the documentation of this file.
5
6from enum import Enum
7from .bridge_loggerinterface import ILogger
8from .bridge_filelogger import FileLogger
9from .bridge_consolelogger import ConsoleLogger
10from .bridge_nulllogger import NullLogger
11
12
17
18
20 class LoggerTypes(Enum):
21
22 ToNull = 0,
23
24
25 ToFile = 1,
26
27
28 ToConsole = 2
29
30
31
40 def __init__(self, loggerType : LoggerTypes, *arguments):
41 self._logger = None
42 match (loggerType):
43 case Logger.LoggerTypes.ToNull:
44 self._logger = NullLogger.CreateLogger()
45
46 case Logger.LoggerTypes.ToConsole:
47 self._logger = ConsoleLogger.CreateLogger()
48
49 case Logger.LoggerTypes.ToFile:
50 if arguments:
51 self._logger = FileLogger.CreateLogger(arguments[0])
52 else:
53 msg = "A filename must be specified for the {0} logger type.".format(loggerType)
54 raise ValueError(msg)
55
56 case _: # default
57 msg = "The logger type '{0}' is not recognized. Cannot construct a Logger.".format(loggerType)
58 raise ValueError(msg)
59
60
62
63
65 def __enter__(self):
66 return self
67
68
69
70 def __exit__(self, *args):
71 if self._logger:
72 self._logger.Close()
73 self._logger = None
74
75 #-------------------------------------------------------------------------
76 # ILogger interface implementation
77 #-------------------------------------------------------------------------
78
79
82 def Close(self):
83 if self._logger:
84 self._logger.Close()
85 self._logger = None
86
87
91 def LogTrace(self, message : str):
92 if self._logger:
93 self._logger.LogTrace(message)
94
95
96
97
101 def LogInfo(self, message : str):
102 if self._logger:
103 self._logger.LogInfo(message)
104
105
106
110 def LogError(self, message : str):
111 if self._logger:
112 self._logger.LogError(message)
113
114
@ Close
Window is asked to close itself, generally sent by the window itself in response to a button up in a ...
A value passed to LoggerClassFactory.CreateLogger() to specify the type of logger to create.
Represents the bridge logger object to be used in the program.
def LogError(self, str message)
Log error messages to the configured output.
def __enter__(self)
Entry function used in the with statement to initialize an instance of the reader/writer.
def LogTrace(self, str message)
Log trace messages to the configured output.
def __init__(self, LoggerTypes loggerType, *arguments)
Constructor that takes a LoggerTypes value and optional arguments to create a new Logger class.
def LogInfo(self, str message)
Log informational messages to the configured output.
def __exit__(self, *args)
Exit function automatically called when used in the with statement.
Represents an implementation of a logger object as called from the Logger class.