Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import logging 

2import sys 

3 

4# This is the root logger of icet 

5logger = logging.getLogger('icet') 

6 

7# Set the format which is simpler for the console 

8FORMAT = '%(name)s: %(levelname)s %(message)s' 

9formatter = logging.Formatter(FORMAT) 

10 

11# Will process all levels of DEBUG or higher 

12logger.setLevel(logging.DEBUG) 

13 

14# If you know what you are doing you may set this to True 

15logger.propagate = False 

16 

17# The logger will collect events from children and the default 

18# behaviour is to print it directly to stdout 

19sh = logging.StreamHandler(sys.stdout) 

20sh.setFormatter(formatter) 

21sh.setLevel(logging.WARNING) 

22logger.addHandler(sh) 

23 

24 

25class MyFilter(logging.Filter): 

26 def __init__(self, level): 

27 self.__level = level 

28 

29 def filter(self, logRecord): 

30 return logRecord.levelno == self.__level 

31 

32 

33def set_log_config(filename=None, level=None, restricted=False): 

34 

35 # If a filename is provided a logfile will be created 

36 if filename is not None: 

37 fh = logging.FileHandler(filename) 

38 fh.setFormatter(formatter) 

39 logger.addHandler(fh) 

40 

41 # Restrict file handler to only write out at the level specified 

42 if restricted: 42 ↛ 43line 42 didn't jump to line 43, because the condition on line 42 was never true

43 lvl = logging.getLevelName(level) 

44 fh.addFilter(MyFilter(lvl)) 

45 

46 if level is not None: 

47 fh.setLevel(level) 

48 

49 # If only level is provided, the stream handler will be reset to this level 

50 elif level is not None: 50 ↛ exitline 50 didn't return from function 'set_log_config', because the condition on line 50 was never false

51 sh.setLevel(level)