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

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

import logging 

import sys 

 

# This is the root logger of icet 

logger = logging.getLogger('icet') 

 

# Set the format which is simpler for the console 

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

formatter = logging.Formatter(FORMAT) 

 

# Will process all levels of DEBUG or higher 

logger.setLevel(logging.DEBUG) 

 

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

logger.propagate = False 

 

# The logger will collect events from children and the default 

# behaviour is to print it directly to stdout 

sh = logging.StreamHandler(sys.stdout) 

sh.setFormatter(formatter) 

sh.setLevel(logging.WARNING) 

logger.addHandler(sh) 

 

 

class MyFilter(logging.Filter): 

def __init__(self, level): 

self.__level = level 

 

def filter(self, logRecord): 

return logRecord.levelno == self.__level 

 

 

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

 

# If a filename is provided a logfile will be created 

if filename is not None: 

fh = logging.FileHandler(filename) 

fh.setFormatter(formatter) 

logger.addHandler(fh) 

 

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

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

lvl = logging.getLevelName(level) 

fh.addFilter(MyFilter(lvl)) 

 

if level is not None: 

fh.setLevel(level) 

 

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

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

sh.setLevel(level)