forked from easybuilders/easybuild-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfancylogger.py
984 lines (801 loc) · 33.8 KB
/
fancylogger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
#
# Copyright 2011-2023 Ghent University
#
# This file is part of EasyBuild,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
# with support of Ghent University (http://ugent.be/hpc),
# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),
# Flemish Research Foundation (FWO) (http://www.fwo.be/en)
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
#
# https://github.com/easybuilders/easybuild
#
# EasyBuild is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation v2.
#
# EasyBuild is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with EasyBuild. If not, see <http://www.gnu.org/licenses/>.
#
"""
This module implements a fancy logger on top of python logging
It adds:
- custom specifiers for mpi logging (the mpirank) with autodetection of mpi
- custom specifier for always showing the calling function's name
- rotating file handler
- a default formatter.
- logging to an UDP server (vsc.logging.logdaemon.py f.ex.)
- easily setting loglevel
- easily add extra specifiers in the log record
- internal debugging through environment variables
FANCYLOGGER_GETLOGGER_DEBUG for getLogger
FANCYLOGGER_LOGLEVEL_DEBUG for setLogLevel
- set FANCYLOGGER_IGNORE_MPI4PY to disable mpi4py module import
mpi4py (when available) is automatically used for mpi-aware log format
In case mpi4py however that it is available but broken,
set this variable to 1 to avoid importing it.
usage:
>>> from easybuild.base import fancylogger
>>> # will log to screen by default
>>> fancylogger.logToFile('dir/filename')
>>> fancylogger.setLogLevelDebug() # set global loglevel to debug
>>> logger = fancylogger.getLogger(name) # get a logger with a specific name
>>> logger.setLevel(level) # set local debugging level
>>> # If you want the logger to be showing modulename.functionname as the name, use
>>> fancylogger.getLogger(fname=True)
>>> # you can use the handler to set a different formatter by using
>>> handler = fancylogger.logToFile('dir/filename')
>>> formatstring = '%(asctime)-15s %(levelname)-10s %(mpirank)-5s %(funcname)-15s %(threadName)-10s %(message)s'
>>> handler.setFormatter(logging.Formatter(formatstring))
>>> # setting a global loglevel will impact all logers:
>>> from easybuild.base import fancylogger
>>> logger = fancylogger.getLogger("test")
>>> logger.warning("warning")
2012-01-05 14:03:18,238 WARNING <stdin>.test.<module> MainThread warning
>>> logger.debug("warning")
>>> fancylogger.setLogLevelDebug()
>>> logger.debug("warning")
2012-01-05 14:03:46,222 DEBUG <stdin>.test.<module> MainThread warning
Logging to a udp server:
- set an environment variable FANCYLOG_SERVER and FANCYLOG_SERVER_PORT (optionally)
- this will make fancylogger log to that that server and port instead of the screen.
Authors:
* Jens Timmerman (Ghent University)
* Stijn De Weirdt (Ghent University)
* Kenneth Hoste (Ghent University)
"""
from collections import namedtuple
import inspect
import logging
import logging.handlers
import os
import sys
import threading
import traceback
import weakref
from easybuild.tools import LooseVersion
def _env_to_boolean(varname, default=False):
"""
Compute a boolean based on the truth value of environment variable `varname`.
If no variable by that name is present in `os.environ`, then return `default`.
For the purpose of this function, the string values ``'1'``,
``'y'``, ``'yes'``, and ``'true'`` (case-insensitive) are all
mapped to the truth value ``True``::
>>> os.environ['NO_FOOBAR'] = '1'
>>> _env_to_boolean('NO_FOOBAR')
True
>>> os.environ['NO_FOOBAR'] = 'Y'
>>> _env_to_boolean('NO_FOOBAR')
True
>>> os.environ['NO_FOOBAR'] = 'Yes'
>>> _env_to_boolean('NO_FOOBAR')
True
>>> os.environ['NO_FOOBAR'] = 'yes'
>>> _env_to_boolean('NO_FOOBAR')
True
>>> os.environ['NO_FOOBAR'] = 'True'
>>> _env_to_boolean('NO_FOOBAR')
True
>>> os.environ['NO_FOOBAR'] = 'TRUE'
>>> _env_to_boolean('NO_FOOBAR')
True
>>> os.environ['NO_FOOBAR'] = 'true'
>>> _env_to_boolean('NO_FOOBAR')
True
Any other value is mapped to Python ``False``::
>>> os.environ['NO_FOOBAR'] = '0'
>>> _env_to_boolean('NO_FOOBAR')
False
>>> os.environ['NO_FOOBAR'] = 'no'
>>> _env_to_boolean('NO_FOOBAR')
False
>>> os.environ['NO_FOOBAR'] = 'if you please'
>>> _env_to_boolean('NO_FOOBAR')
False
If no variable named `varname` is present in `os.environ`, then
return `default`::
>>> del os.environ['NO_FOOBAR']
>>> _env_to_boolean('NO_FOOBAR', 42)
42
By default, calling `_env_to_boolean` on an undefined
variable returns Python ``False``::
>>> if 'NO_FOOBAR' in os.environ: del os.environ['NO_FOOBAR']
>>> _env_to_boolean('NO_FOOBAR')
False
"""
if varname not in os.environ:
return default
else:
return os.environ.get(varname).lower() in ('1', 'yes', 'true', 'y')
OPTIMIZED_ANSWER = "not available in optimized mode"
HAVE_COLOREDLOGS_MODULE = False
if not _env_to_boolean('FANCYLOGGER_NO_COLOREDLOGS'):
try:
import coloredlogs
import humanfriendly
HAVE_COLOREDLOGS_MODULE = True
except ImportError:
pass
# constants
TEST_LOGGING_FORMAT = '%(levelname)-10s %(name)-15s %(threadName)-10s %(message)s'
DEFAULT_LOGGING_FORMAT = '%(asctime)-15s ' + TEST_LOGGING_FORMAT
DEFAULT_LOGGING_FORMAT_MPI = '%(asctime)-15s %(levelname)-10s %(name)-15s' \
' mpi: %(mpirank)s %(threadName)-10s %(message)s'
MPIRANK_NO_MPI = "N/A"
# keep the original logging root logger around for reset purposes
# rootlogger always has a loglevel
_orig_logging_root = [logging.root, logging.root.level, logging.root.handlers[:]]
FANCYLOG_LOGGING_FORMAT = None
FANCYLOG_FANCYRECORD = None
# DEFAULT_LOGGING_FORMAT= '%(asctime)-15s %(levelname)-10s %(module)-15s %(threadName)-10s %(message)s'
MAX_BYTES = 100 * 1024 * 1024 # max bytes in a file with rotating file handler
BACKUPCOUNT = 10 # number of rotating log files to save
DEFAULT_UDP_PORT = 5005
# poor man's enum
Colorize = namedtuple('Colorize', 'AUTO ALWAYS NEVER')('auto', 'always', 'never')
APOCALYPTIC = 'APOCALYPTIC'
# register new loglevelname
logging.addLevelName(logging.CRITICAL * 2 + 1, APOCALYPTIC)
# mpi rank support
_MPIRANK = MPIRANK_NO_MPI
if not _env_to_boolean('FANCYLOGGER_IGNORE_MPI4PY'):
try:
from mpi4py import MPI
if MPI.Is_initialized():
_MPIRANK = str(MPI.COMM_WORLD.Get_rank())
if MPI.COMM_WORLD.Get_size() > 1:
# enable mpi rank when mpi is used
FANCYLOG_FANCYRECORD = True
DEFAULT_LOGGING_FORMAT = DEFAULT_LOGGING_FORMAT_MPI
except ImportError:
pass
class MissingLevelName(KeyError):
pass
def getLevelInt(level_name):
"""Given a level name, return the int value"""
if not isinstance(level_name, str):
raise TypeError('Provided name %s is not a string (type %s)' % (level_name, type(level_name)))
level = logging.getLevelName(level_name)
if isinstance(level, str):
raise MissingLevelName('Unknown loglevel name %s' % level_name)
return level
class FancyStreamHandler(logging.StreamHandler):
"""The logging StreamHandler with uniform named arg in __init__ for selecting the stream."""
def __init__(self, stream=None, stdout=None):
"""Initialize the stream (default is sys.stderr)
- stream : a specific stream to use
- stdout: if True and no stream specified, set stream to sys.stdout (False log to stderr)
"""
logging.StreamHandler.__init__(self)
if stream is not None:
pass
elif stdout is False or stdout is None:
stream = sys.stderr
elif stdout is True:
stream = sys.stdout
self.stream = stream
class FancyLogRecord(logging.LogRecord):
"""
This class defines a custom log record.
Adding extra specifiers is as simple as adding attributes to the log record
"""
def __init__(self, *args, **kwargs):
logging.LogRecord.__init__(self, *args, **kwargs)
# modify custom specifiers here
# we won't do this when running with -O, becuase this might be a heavy operation
# the __debug__ operation is actually recognised by the python compiler and it won't even do a single comparison
if __debug__:
self.className = _getCallingClassName(depth=5)
else:
self.className = 'N/A'
self.mpirank = _MPIRANK
# Custom logger that uses our log record
class FancyLogger(logging.getLoggerClass()):
"""
This is a custom Logger class that uses the FancyLogRecord
and has extra log methods raiseException and deprecated and
streaming versions for debug,info,warning and error.
"""
# this attribute can be checked to know if the logger is thread aware
_thread_aware = True
# default class for raiseException method, that can be redefined by deriving loggers
RAISE_EXCEPTION_CLASS = Exception
def log_method(self, msg):
self.warning(msg)
RAISE_EXCEPTION_LOG_METHOD = log_method
# method definition as it is in logging, can't change this
# pylint: disable=unused-argument
def makeRecord(self, name, level, pathname, lineno, msg, args, excinfo, func=None, extra=None, sinfo=None):
"""
overwrite make record to use a fancy record (with more options)
"""
logrecordcls = logging.LogRecord
if hasattr(self, 'fancyrecord') and self.fancyrecord:
logrecordcls = FancyLogRecord
try:
new_msg = str(msg)
except UnicodeEncodeError:
new_msg = msg.encode('utf8', 'replace')
return logrecordcls(name, level, pathname, lineno, new_msg, args, excinfo)
def fail(self, message, *args):
"""Log error message and raise exception."""
formatted_message = message % args
self.RAISE_EXCEPTION_LOG_METHOD(formatted_message)
raise self.RAISE_EXCEPTION_CLASS(formatted_message)
def raiseException(self, message, exception=None, catch=False):
"""
logs message and raises an exception (since it can be caught higher up and handled)
and raises it afterwards
:param exception: subclass of Exception to use for raising
:param catch: boolean, try to catch raised exception and add relevant info to message
(this will also happen if exception is not specified)
"""
fullmessage = message
tb = None
if catch or exception is None:
# assumes no control by codemonkey
# lets see if there is something more to report on
exc, detail, tb = sys.exc_info()
if exc is not None:
if exception is None:
exception = exc
# extend the message with the traceback and some more details
# or use self.exception() instead of self.warning()?
tb_text = "\n".join(traceback.format_tb(tb))
message += " (%s)" % detail
fullmessage += " (%s\n%s)" % (detail, tb_text)
if exception is None:
exception = self.RAISE_EXCEPTION_CLASS
self.RAISE_EXCEPTION_LOG_METHOD(fullmessage)
raise exception(message).with_traceback(tb)
# pylint: disable=unused-argument
def deprecated(self, msg, cur_ver, max_ver, depth=2, exception=None, log_callback=None, *args, **kwargs):
"""
Log deprecation message, throw error if current version is passed given threshold.
Checks only major/minor version numbers (MAJ.MIN.x) by default, controlled by 'depth' argument.
"""
if log_callback is None:
log_callback = self.warning
loose_cv = LooseVersion(cur_ver)
loose_mv = LooseVersion(max_ver)
if loose_cv.version[:depth] >= loose_mv.version[:depth]:
self.raiseException("DEPRECATED (since v%s) functionality used: %s" % (max_ver, msg), exception=exception)
else:
deprecation_msg = "Deprecated functionality, will no longer work in v%s: %s" % (max_ver, msg)
log_callback(deprecation_msg)
def _handleFunction(self, function, levelno, **kwargs):
"""
Walk over all handlers like callHandlers and execute function on each handler
"""
c = self
found = 0
while c:
for hdlr in c.handlers:
found = found + 1
if levelno >= hdlr.level:
function(hdlr, **kwargs)
if not c.propagate:
c = None # break out
else:
c = c.parent
def setLevelName(self, level_name):
"""Set the level by name."""
# This is supported in py27 setLevel code, but not in py24
self.setLevel(getLevelInt(level_name))
def streamLog(self, levelno, data):
"""
Add (continuous) data to an existing message stream (eg a stream after a logging.info()
"""
if isinstance(levelno, str):
levelno = getLevelInt(levelno)
def write_and_flush_stream(hdlr, data=None):
"""Write to stream and flush the handler"""
if getattr(hdlr, 'stream', None) is None:
# no stream or not initialised.
raise ValueError("write_and_flush_stream failed. No active stream attribute.")
if data is not None:
hdlr.stream.write(data)
hdlr.flush()
# only log when appropriate (see logging.Logger.log())
if self.isEnabledFor(levelno):
self._handleFunction(write_and_flush_stream, levelno, data=data)
def streamDebug(self, data):
"""Get a DEBUG loglevel streamLog"""
self.streamLog('DEBUG', data)
def streamInfo(self, data):
"""Get a INFO loglevel streamLog"""
self.streamLog('INFO', data)
def streamError(self, data):
"""Get a ERROR loglevel streamLog"""
self.streamLog('ERROR', data)
def _get_parent_info(self, verbose=True):
"""Return some logger parent related information"""
def info(x):
res = [x, x.name, logging.getLevelName(x.getEffectiveLevel()), logging.getLevelName(x.level), x.disabled]
if verbose:
res.append([(h, logging.getLevelName(h.level)) for h in x.handlers])
return res
parentinfo = []
logger = self
parentinfo.append(info(logger))
while logger.parent is not None:
logger = logger.parent
parentinfo.append(info(logger))
return parentinfo
def get_parent_info(self, prefix, verbose=True):
"""Return pretty text version"""
rev_parent_info = self._get_parent_info(verbose=verbose)
return ["%s %s%s" % (prefix, " " * 4 * idx, info) for idx, info in enumerate(rev_parent_info)]
def __copy__(self):
"""Return shallow copy, in this case reference to current logger"""
return getLogger(self.name, fname=False, clsname=False)
def __deepcopy__(self, memo):
"""This behaviour is undefined, fancylogger will return shallow copy, instead just crashing."""
return self.__copy__()
def thread_name():
"""
returns the current threads name
"""
return threading.currentThread().getName()
def getLogger(name=None, fname=False, clsname=False, fancyrecord=None):
"""
Returns a Fancylogger instance
if fname is True, the loggers name will be 'name[.classname].functionname'
if clsname is True the loggers name will be 'name.classname[.functionname]'
This will return a logger with a fancylog record, which includes the className template for the logformat
This can make your code a lot slower, so this can be disabled by setting fancyrecord or class module
FANCYLOG_FANCYRECORD to False, or will also be disabled if a Name is set (and fancyrecord and
module constant FANCYLOG_FANCYRECORD are also not set).
"""
nameparts = []
if not is_fancyroot():
# deliberately not calling getRootLoggerName function to determine actual root logger name,
# because it is prohibitively expensive in some texts (even when using 'python -O')
nameparts.append('fancyroot')
if fancyrecord is None:
# Altough we could set it as default value in the function definition
# it's easier to explain if we do it this way
fancyrecord = FANCYLOG_FANCYRECORD
if name:
nameparts.append(name)
elif fancyrecord is None: # only be fancy if fancyrecord is True or no name is given
fancyrecord = True
fancyrecord = bool(fancyrecord) # make sure fancyrecord is a nice bool, not None
if clsname:
nameparts.append(_getCallingClassName())
if fname:
nameparts.append(_getCallingFunctionName())
fullname = ".".join(nameparts)
log = logging.getLogger(fullname)
log.fancyrecord = fancyrecord
if _env_to_boolean('FANCYLOGGER_GETLOGGER_DEBUG'):
sys.stdout.write('FANCYLOGGER_GETLOGGER_DEBUG')
sys.stdout.write('name ' + name + ' fname ' + fname + ' fullname' + fullname)
sys.stdout.write("getRootLoggerName: %s\n" % getRootLoggerName())
if hasattr(log, 'get_parent_info'):
sys.stdout.write('parent_info verbose\n')
sys.stdout.write('\n'.join(log.get_parent_info('FANCYLOGGER_GETLOGGER_DEBUG')) + '\n')
sys.stdout.flush()
return log
def _getCallingFunctionName():
"""
returns the name of the function calling the function calling this function
(for internal use only)
"""
if __debug__:
try:
return inspect.stack()[2][3]
except Exception:
return "unknown__getCallingFunctionName"
else:
return OPTIMIZED_ANSWER
def _getCallingClassName(depth=2):
"""
returns the name of the class calling the function calling this function
(for internal use only)
"""
if __debug__:
try:
return inspect.stack()[depth][0].f_locals['self'].__class__.__name__
except Exception:
return "unknown__getCallingClassName"
else:
return OPTIMIZED_ANSWER
def getRootLoggerName():
"""
returns the name of the root module
this is the module that is actually running everything and so doing the logging
"""
if __debug__:
try:
return inspect.stack()[-1][1].split('/')[-1].split('.')[0]
except Exception:
return "unknown_getRootLoggerName"
else:
return OPTIMIZED_ANSWER
def logToScreen(enable=True, handler=None, name=None, stdout=False, colorize=Colorize.NEVER):
"""
enable (or disable) logging to screen
returns the screenhandler (this can be used to later disable logging to screen)
if you want to disable logging to screen, pass the earlier obtained screenhandler
you can also pass the name of the logger for which to log to the screen
otherwise you'll get all logs on the screen
by default, logToScreen will log to stderr; logging to stdout instead can be done
by setting the 'stdout' parameter to True
The `colorize` parameter enables or disables log colorization using
ANSI terminal escape sequences, according to the values allowed
in the `colorize` parameter to function `_screenLogFormatterFactory`
(which see).
"""
handleropts = {'stdout': stdout}
formatter = _screenLogFormatterFactory(colorize=colorize, stream=(sys.stdout if stdout else sys.stderr))
return _logToSomething(FancyStreamHandler,
handleropts,
loggeroption='logtoscreen_stdout_%s' % str(stdout),
name=name,
enable=enable,
handler=handler,
formatterclass=formatter,
)
def logToFile(filename, enable=True, filehandler=None, name=None, max_bytes=MAX_BYTES, backup_count=BACKUPCOUNT):
"""
enable (or disable) logging to file
given filename
will log to a file with the given name using a rotatingfilehandler
this will let the file grow to MAX_BYTES and then rotate it
saving the last BACKUPCOUNT files.
returns the filehandler (this can be used to later disable logging to file)
if you want to disable logging to file, pass the earlier obtained filehandler
"""
handleropts = {
'filename': filename,
'mode': 'a',
'maxBytes': max_bytes,
'backupCount': backup_count,
}
if sys.version_info[0] >= 3:
handleropts['encoding'] = 'utf-8'
# logging to a file is going to create the file later on, so let's try to be helpful and create the path if needed
directory = os.path.dirname(filename)
if not os.path.exists(directory):
try:
os.makedirs(directory)
except Exception as ex:
exc, detail, tb = sys.exc_info()
raise exc("Cannot create logdirectory %s: %s \n detail: %s" % (directory, ex, detail)).with_traceback(tb)
return _logToSomething(
logging.handlers.RotatingFileHandler,
handleropts,
loggeroption='logtofile_%s' % filename,
name=name,
enable=enable,
handler=filehandler,
)
def logToUDP(hostname, port=5005, enable=True, datagramhandler=None, name=None):
"""
enable (or disable) logging to udp
given hostname and port.
returns the filehandler (this can be used to later disable logging to udp)
if you want to disable logging to udp, pass the earlier obtained filehandler,
and set boolean = False
"""
handleropts = {'hostname': hostname, 'port': port}
return _logToSomething(logging.handlers.DatagramHandler,
handleropts,
loggeroption='logtoudp_%s:%s' % (hostname, str(port)),
name=name,
enable=enable,
handler=datagramhandler,
)
def _logToSomething(handlerclass, handleropts, loggeroption,
enable=True, name=None, handler=None, formatterclass=None):
"""
internal function to enable (or disable) logging to handler named handlername
handleropts is options dictionary passed to create the handler instance;
`formatterclass` is the class to use to instantiate a log formatter object.
returns the handler (this can be used to later disable logging to file)
if you want to disable logging to the handler, pass the earlier obtained handler
"""
logger = getLogger(name, fname=False, clsname=False)
if formatterclass is None:
formatterclass = logging.Formatter
if not hasattr(logger, loggeroption):
# not set.
setattr(logger, loggeroption, False) # set default to False
if enable:
if not getattr(logger, loggeroption):
if handler is None:
if FANCYLOG_LOGGING_FORMAT is None:
f_format = DEFAULT_LOGGING_FORMAT
else:
f_format = FANCYLOG_LOGGING_FORMAT
formatter = formatterclass(f_format)
handler = handlerclass(**handleropts)
handler.setFormatter(formatter)
logger.addHandler(handler)
setattr(logger, loggeroption, handler)
else:
handler = getattr(logger, loggeroption)
elif not enable:
# stop logging to X
if handler is None:
if len(logger.handlers) == 1:
# removing the last logger doesn't work
# it will be re-added if only one handler is present
# so we will just make it quiet by setting the loglevel extremely high
zerohandler = logger.handlers[0]
# no logging should be done with APOCALYPTIC, so silence happens
zerohandler.setLevel(getLevelInt(APOCALYPTIC))
else: # remove the handler set with this loggeroption
handler = getattr(logger, loggeroption)
logger.removeHandler(handler)
if hasattr(handler, 'close') and callable(handler.close):
handler.close()
else:
logger.removeHandler(handler)
setattr(logger, loggeroption, False)
return handler
def _screenLogFormatterFactory(colorize=Colorize.NEVER, stream=sys.stdout):
"""
Return a log formatter class, with optional colorization features.
Second argument `colorize` controls whether the formatter
can use ANSI terminal escape sequences:
* ``Colorize.NEVER`` (default) forces use the plain `logging.Formatter` class;
* ``Colorize.ALWAYS`` forces use of the colorizing formatter;
* ``Colorize.AUTO`` selects the colorizing formatter depending on
whether `stream` is connected to a terminal.
Second argument `stream` is the stream to check in case `colorize`
is ``Colorize.AUTO``.
"""
formatter = logging.Formatter # default
if HAVE_COLOREDLOGS_MODULE:
if colorize == Colorize.AUTO:
# auto-detect
if humanfriendly.terminal.terminal_supports_colors(stream):
formatter = coloredlogs.ColoredFormatter
elif colorize == Colorize.ALWAYS:
formatter = coloredlogs.ColoredFormatter
elif colorize == Colorize.NEVER:
pass
else:
raise ValueError("Argument `colorize` must be one of 'auto', 'always', or 'never'.")
return formatter
def _getSysLogFacility(name=None):
"""Look for proper syslog facility
typically the syslog/rsyslog config has an entry
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages
name -> LOG_%s % name.upper()
Default log facility is user /LOG_USER
"""
if name is None:
name = 'user'
facility = getattr(logging.handlers.SysLogHandler,
"LOG_%s" % name.upper(), logging.handlers.SysLogHandler.LOG_USER)
return facility
def logToDevLog(enable=True, name=None, handler=None):
"""Log to syslog through /dev/log"""
devlog = '/dev/log'
syslogoptions = {
'address': devlog,
'facility': _getSysLogFacility()
}
return _logToSomething(logging.handlers.SysLogHandler,
syslogoptions, 'logtodevlog', enable=enable, name=name, handler=handler)
# Change loglevel
def setLogLevel(level):
"""
Set a global log level for all FancyLoggers
"""
if isinstance(level, str):
level = getLevelInt(level)
logger = getLogger(fname=False, clsname=False)
logger.setLevel(level)
if _env_to_boolean('FANCYLOGGER_LOGLEVEL_DEBUG'):
sys.stdout.write('FANCYLOGGER_LOGLEVEL_DEBUG ' + level + ' ' + logging.getLevelName(level) + '\n')
sys.stdout.write('\n'.join(logger.get_parent_info('FANCYLOGGER_LOGLEVEL_DEBUG')) + '\n')
sys.stdout.flush()
def setLogLevelDebug():
"""
shorthand for setting debug level
"""
setLogLevel('DEBUG')
def setLogLevelInfo():
"""
shorthand for setting loglevel to Info
"""
setLogLevel('INFO')
def setLogLevelWarning():
"""
shorthand for setting loglevel to Warning
"""
setLogLevel('WARNING')
def setLogLevelError():
"""
shorthand for setting loglevel to Error
"""
setLogLevel('ERROR')
def getAllExistingLoggers():
"""
:return: the existing loggers, in a list of C{(name, logger)} tuples
"""
# not-so-well documented manager (in 2.6 and later)
# return list of (name,logger) tuple
return [x for x in logging.Logger.manager.loggerDict.items()] + [(logging.root.name, logging.root)]
def getAllNonFancyloggers():
"""
:return: all loggers that are not fancyloggers
"""
return [x for x in getAllExistingLoggers() if not isinstance(x[1], FancyLogger)]
def getAllFancyloggers():
"""
Return all loggers that are not fancyloggers
"""
return [x for x in getAllExistingLoggers() if isinstance(x[1], FancyLogger)]
def setLogFormat(f_format):
"""Set the log format. (Has to be set before logToSomething is called)."""
global FANCYLOG_LOGGING_FORMAT
FANCYLOG_LOGGING_FORMAT = f_format
def setTestLogFormat():
"""Set the log format to the test format (i.e. without timestamp)."""
setLogFormat(TEST_LOGGING_FORMAT)
def is_fancyroot():
"""
Return if the logging.root logger is a FancyLogger
"""
return isinstance(logging.root, FancyLogger)
def setroot(fancyrecord=FANCYLOG_FANCYRECORD):
"""
Set a FancyLogger instance as the logging root logger
with (effective)loglevel of current root FancyLogger
:param fancyrecord: is enabled or not (default FANCYLOG_FANCYRECORD module constant)
Detecting the loglevel is best-effort, better to set the loglevel after setroot()
"""
if is_fancyroot():
return
class FancyRootLogger(FancyLogger, logging.RootLogger):
__init__ = logging.RootLogger.__init__
# current root FancyLogger
logger = getLogger(fname=False, clsname=False)
lvl = logger.getEffectiveLevel()
# Disable dedicated level, follows root level now
logger.level = logging.NOTSET
root = FancyRootLogger(lvl)
root.fancyrecord = fancyrecord
# make copy, instead of the reference, because we are going to delete stuff
handlers = getattr(logger, 'handlers', [])[:]
if handlers:
# Move all existing handlers from root fancylogger to new root
# getLogger() call used in logToSomething will retrun the root
for hndlr in handlers:
root.addHandler(hndlr)
logger.removeHandler(hndlr)
else:
handlers = getattr(logging.root, 'handlers', [])
# only copy the logging.root handlers
for hndlr in handlers:
root.addHandler(hndlr)
# Swap out logging.root parent for all existing loggers
for lgr in getAllExistingLoggers():
# PlaceHolders have no parent
if hasattr(lgr[1], 'parent') and lgr[1].parent == logging.root:
lgr[1].parent = root
if _env_to_boolean('FANCYLOGGER_LOGLEVEL_DEBUG'):
sys.stdout.write('FANCYLOGGER_LOGLEVEL_DEBUG SETROOT ' + lvl + ' ' + logging.getLevelName(lvl) + '\n')
sys.stdout.write('\n'.join(root.get_parent_info("FANCYLOGGER_LOGLEVEL_DEBUG SETROOT ")) + '\n')
sys.stdout.flush()
# silence the root logger
_orig_logging_root[1] = logging.root.level
_orig_logging_root[2] = logging.root.handlers[:]
logging.root.setLevel(getLevelInt(APOCALYPTIC))
logging.root = root
logging.Logger.root = root
# Do not re-init the manager
logging.Logger.manager.root = root
def resetroot():
"""
Restore the original logging.root logger
"""
if not is_fancyroot():
return
root = _orig_logging_root[0]
root.setLevel(_orig_logging_root[1])
root.handlers = _orig_logging_root[2]
# Swap out logging.root parent for all existing loggers
for lgr in getAllExistingLoggers():
# PlaceHolders have no parent
if hasattr(lgr[1], 'parent') and lgr[1].parent == logging.root:
lgr[1].parent = root
logging.root = root
logging.Logger.root = root
logging.Logger.manager.root = root
# Register our logger
logging.setLoggerClass(FancyLogger)
# log to a server if FANCYLOG_SERVER is set.
_default_logTo = None
if 'FANCYLOG_SERVER' in os.environ:
server = os.environ['FANCYLOG_SERVER']
port = DEFAULT_UDP_PORT
if ':' in server:
server, port = server.split(':')
# maybe the port was specified in the FANCYLOG_SERVER_PORT env var. this takes precedence
if 'FANCYLOG_SERVER_PORT' in os.environ:
port = int(os.environ['FANCYLOG_SERVER_PORT'])
port = int(port)
logToUDP(server, port)
_default_logTo = logToUDP
else:
# log to screen by default
logToScreen(enable=True)
_default_logTo = logToScreen
_default_handlers = logging._handlerList[:] # There's always one
def _enable_disable_default_handlers(enable):
"""Interact with the default handlers to enable or disable them"""
if _default_logTo is None:
return
for hndlr in _default_handlers:
# py2.7 are weakrefs, 2.6 not
if isinstance(hndlr, weakref.ref):
handler = hndlr()
else:
handler = hndlr
try:
_default_logTo(enable=enable, handler=handler)
except Exception:
pass
def disableDefaultHandlers():
"""Disable the default handlers on all fancyloggers
- if this is the last logger, it will just set the logLevel very high
"""
_enable_disable_default_handlers(False)
def enableDefaultHandlers():
"""(re)Enable the default handlers on all fancyloggers"""
_enable_disable_default_handlers(True)
def getDetailsLogLevels(fancy=True, numeric=False):
"""
Return list of (name,loglevelname) pairs of existing loggers
:param fancy: if True, returns only Fancylogger; if False, returns non-FancyLoggers,
anything else, return all loggers
:param numeric: if True, return the numeric value instead of the name
"""
func_map = {
True: getAllFancyloggers,
False: getAllNonFancyloggers,
}
func = func_map.get(fancy, getAllExistingLoggers)
res = []
for name, logger in func():
# PlaceHolder instances have no level attribute set
level_value = getattr(logger, 'level', logging.NOTSET)
if numeric:
level_name = level_value
else:
level_name = logging.getLevelName(level_value)
res.append((name, level_name))
return res