pyenv-virtualenv for Windows 1.2
A 'pyenv' plugin to manage Python virtual environments, depending on different Python versions, for various Python projects.
Loading...
Searching...
No Matches
log Namespace Reference

Functions

 initLogging ()
 Initialize the logging.
 
bool isInitialized ()
 
str getExcInfoStr (tuple exc_info)
 Parse the exception data for human-readable information.
 
str getMsgText ((str, tuple) msg)
 Get text string from "msg" object argument.
 
 printLogToConsole (int level,(str, tuple) msg)
 Log leveled and colored message to console only.
 
 critical ((str, tuple) msg)
 Log critical error message colored to console only.
 
 error ((str, tuple) msg)
 Log error message colored to console only.
 
 success ((str, tuple) msg)
 Log success message colored to console only.
 
 warning ((str, tuple) msg)
 Log warning message colored to console only.
 
 notice ((str, tuple) msg)
 Log notice message colored to console only.
 
 info ((str, tuple) msg)
 Log info message colored to console only.
 
 verbose ((str, tuple) msg)
 Log verbose message colored to console only.
 
 debug ((str, tuple) msg)
 Log debug message colored to console only.
 
 spam ((str, tuple) msg)
 Log spam message colored to console only.
 

Variables

list LOG_LEVELS
 List of log level attributes for comprehensive logging.
 
int LOG_LEVEL = 20
 Actual log level.
 
int LEVEL_COLUMN_WIDTH = 0
 Width of the column, which shows the log level name.
 
bool _is_initialized = False
 Flag, which states, that the logging is initialized for this program.
 
 UnexpExcInfo
 Named tuple definition of unexpected exception.
 

Function Documentation

◆ initLogging()

log.initLogging ( )

Initialize the logging.

Definition at line 71 of file log.py.

71def initLogging():
72 # Define global variables
73 global LOG_LEVEL
74 global LEVEL_COLUMN_WIDTH
75 # Automatically calculate LEVEL_COLUMN_WIDTH
76 for item in LOG_LEVELS:
77 lln = len(item['name'])
78 if LEVEL_COLUMN_WIDTH < lln:
79 LEVEL_COLUMN_WIDTH = lln
80 # Override LOG_LEVEL by environment variable
81 if 'LOG_LEVEL' in os.environ:
82 LOG_LEVEL = int(os.environ['LOG_LEVEL'])
83 # Set logging is initialized
84 global _is_initialized
85 _is_initialized = True
86
87# Check if logging is initialized.
88#
89# return Flag, which states that logging is initialized.

◆ isInitialized()

bool log.isInitialized ( )

Definition at line 90 of file log.py.

90def isInitialized() -> bool:
91 return _is_initialized
92

◆ getExcInfoStr()

str log.getExcInfoStr ( tuple exc_info)

Parse the exception data for human-readable information.

This allows the developer to identify and locate the exception.

Parameters
exc_infoOutput of sys.exc_info().
Returns
Human-readable information or empty string if

Definition at line 99 of file log.py.

99def getExcInfoStr(exc_info: tuple) -> str:
100 maj, mno, _ = platform.python_version_tuple()
101 maj = int(maj)
102 mno = int(mno)
103 ty, va, tb = exc_info
104 if (maj > 3) or ((maj == 3) and (mno >= 11)):
105 qnm = tb.tb_frame.f_code.co_qualname
106 else:
107 qnm = tb.tb_frame.f_code.co_name
108 i = UnexpExcInfo(
109 ty.__name__,
110 va,
111 tb.tb_frame.f_code.co_filename,
112 tb.tb_lineno,
113 qnm,
114 tb
115 )
116 tbs =''.join(traceback.extract_tb(i.tbk).format())
117 return (
118 'Unexpected {}, "{}"\n'.format(i.typ, i.val) +
119 '\x20\x20File: "{}:{}"\n'.format(i.fil, i.lin) +
120 '\x20\x20Pos.: {}, line {}\n'.format(i.qnm, i.lin) +
121 'Traceback (most recent call last):\n' +
122 '{}'.format(tbs)
123 )
124

◆ getMsgText()

str log.getMsgText ( (str, tuple) msg)

Get text string from "msg" object argument.

Parameters
msgMessage as str or result of "sys.exc_info()" as tuple.
Returns
Human-readable message.

Definition at line 129 of file log.py.

129def getMsgText(msg: (str, tuple)) -> str:
130 if isinstance(msg, str):
131 return msg
132 elif isinstance(msg, tuple):
133 return getExcInfoStr(msg)
134 else:
135 raise NotImplementedError
136

◆ printLogToConsole()

log.printLogToConsole ( int level,
(str, tuple) msg )

Log leveled and colored message to console only.

Parameters
levelID of logging level.
See also
LOG_LEVELS
Parameters
msgMessage as str or result of "sys.exc_info()" as tuple.

Definition at line 142 of file log.py.

142def printLogToConsole(level: int, msg: (str, tuple)):
143 for item in LOG_LEVELS:
144 if (
145 (level == item['level'])
146 and
147 (level >= LOG_LEVEL)
148 ):
149 # Print colorized and immediately flush the output
150 print(
151 '{}{}{}{}\x1b[0m'.format(
152 item['color'],
153 item['name'].upper(),
154 ' ' * (
155 LEVEL_COLUMN_WIDTH
156 -
157 len(item['name'])
158 +
159 1
160 ),
161 getMsgText(msg)
162 ),
163 flush=True
164 )
165 return
166
167
168# --- LOGING FUNCTIONS -------------------------------------------------
169

◆ critical()

log.critical ( (str, tuple) msg)

Log critical error message colored to console only.

Parameters
msgMessage as str or result of sys.exc_info().

Definition at line 173 of file log.py.

173def critical(msg: (str, tuple)):
174 printLogToConsole(50, msg)
175

◆ error()

log.error ( (str, tuple) msg)

Log error message colored to console only.

Parameters
msgMessage as str or result of sys.exc_info().

Definition at line 179 of file log.py.

179def error(msg: (str, tuple)):
180 printLogToConsole(40, msg)
181

◆ success()

log.success ( (str, tuple) msg)

Log success message colored to console only.

Parameters
msgMessage as str or result of sys.exc_info().

Definition at line 185 of file log.py.

185def success(msg: (str, tuple)):
186 printLogToConsole(35, msg)
187

◆ warning()

log.warning ( (str, tuple) msg)

Log warning message colored to console only.

Parameters
msgMessage as str or result of sys.exc_info().

Definition at line 191 of file log.py.

191def warning(msg: (str, tuple)):
192 printLogToConsole(30, msg)
193

◆ notice()

log.notice ( (str, tuple) msg)

Log notice message colored to console only.

Parameters
msgMessage as str or result of sys.exc_info().

Definition at line 197 of file log.py.

197def notice(msg: (str, tuple)):
198 printLogToConsole(25, msg)
199

◆ info()

log.info ( (str, tuple) msg)

Log info message colored to console only.

Parameters
msgMessage as str or result of sys.exc_info().

Definition at line 203 of file log.py.

203def info(msg: (str, tuple)):
204 printLogToConsole(20, msg)
205

◆ verbose()

log.verbose ( (str, tuple) msg)

Log verbose message colored to console only.

Parameters
msgMessage as str or result of sys.exc_info().

Definition at line 209 of file log.py.

209def verbose(msg: (str, tuple)):
210 printLogToConsole(15, msg)
211

◆ debug()

log.debug ( (str, tuple) msg)

Log debug message colored to console only.

Parameters
msgMessage as str or result of sys.exc_info().

Definition at line 215 of file log.py.

215def debug(msg: (str, tuple)):
216 printLogToConsole(10, msg)
217

◆ spam()

log.spam ( (str, tuple) msg)

Log spam message colored to console only.

Parameters
msgMessage as str or result of sys.exc_info().

Definition at line 221 of file log.py.

221def spam(msg: (str, tuple)):
222 printLogToConsole(5, msg)
223
224
225# --- END OF CODE ------------------------------------------------------
226

Variable Documentation

◆ LOG_LEVELS

list log.LOG_LEVELS
Initial value:
1= [
2 {'level': 50, 'name': 'critical', 'color': '\x1b[101m'},
3 {'level': 40, 'name': 'error', 'color': '\x1b[91m'},
4 {'level': 35, 'name': 'success', 'color': '\x1b[92m'},
5 {'level': 30, 'name': 'warning', 'color': '\x1b[93m'},
6 {'level': 25, 'name': 'notice', 'color': '\x1b[95m'},
7 {'level': 20, 'name': 'info', 'color': '\x1b[37m'},
8 {'level': 15, 'name': 'verbose', 'color': '\x1b[94m'},
9 {'level': 10, 'name': 'debug', 'color': '\x1b[32m'},
10 {'level': 5, 'name': 'spam', 'color': '\x1b[90m'}
11]

List of log level attributes for comprehensive logging.

NOTE: Color of level 20 is default color in terminals with black background like e.g. the cmd.exe window in Windows.

See also
Logging

Definition at line 34 of file log.py.

◆ LOG_LEVEL

int log.LOG_LEVEL = 20

Actual log level.

It could be overridden by environment variable. Default: 20 (name: 'info')

Definition at line 48 of file log.py.

◆ LEVEL_COLUMN_WIDTH

int log.LEVEL_COLUMN_WIDTH = 0

Width of the column, which shows the log level name.

Default: 0. It will be calculated automatically.

Definition at line 52 of file log.py.

◆ _is_initialized

bool log._is_initialized = False
protected

Flag, which states, that the logging is initialized for this program.

Definition at line 57 of file log.py.

◆ UnexpExcInfo

log.UnexpExcInfo
Initial value:
1= collections.namedtuple(
2 'ExceptionInfo',
3 ['typ', 'val', 'fil', 'lin', 'qnm', 'tbk']
4)

Named tuple definition of unexpected exception.

Definition at line 62 of file log.py.