10 examples of 'python logging to stdout' in Python

Every line of 'python logging to stdout' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
34def logging_both_to_stdout_and_stderr(self, *messages):
35 for index, msg in enumerate(messages):
36 stream = sys.stdout if index % 2 == 0 else sys.stderr
37 stream.write(msg)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

291def setup_logging_to_console(log_obj, level):
292 hdlr = logging.StreamHandler()
293 formatter = logging.Formatter('%(name)s %(message)s')
294 add_log_handler(log_obj, level, hdlr, formatter)
21def configure_logger(output_filename=None):
22 logger = logging.getLogger("neuralnilm")
23 if not logger.handlers:
24 formatter = logging.Formatter('%(asctime)s %(message)s')
25 if output_filename:
26 fh = logging.FileHandler(output_filename)
27 fh.setFormatter(formatter)
28 logger.addHandler(fh)
29 if not is_running_in_ipython_notebook():
30 logger.addHandler(logging.StreamHandler(stream=stdout))
31 logger.setLevel(logging.DEBUG)
65def consolelog(level=logging.WARNING, loggername=__package__, tty=None):
66 h = logging.StreamHandler(tty)
67 h.setLevel(level)
68 formatter = logging.Formatter('%(name)s %(levelname)s: %(message)s')
69 h.setFormatter(formatter)
70 logger = logging.getLogger(loggername)
71 if level < logger.getEffectiveLevel():
72 logger.setLevel(level)
73 logger.addHandler(h)
139def redirectStdout(logger):
140 """Redirect stdout and stderr to a given logger.
141 @param logger: The logger to which to redirect.
142 @type logger: L{Logger}
143 """
144 sys.stdout = StreamRedirector("stdout", logger, logging.WARNING)
145 sys.stderr = StreamRedirector("stderr", logger, logging.ERROR)
31def set_logging():
32 """
33 Setup logging.
34 """
35 root = logging.getLogger()
36 root.setLevel(logging.DEBUG)
37 ch = logging.StreamHandler(sys.stdout)
38 ch.setLevel(logging.DEBUG)
39 formatter = logging.Formatter(
40 '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
41 )
42 ch.setFormatter(formatter)
43 root.addHandler(ch)
15def setup_logger(to_file=LOG['TO_FILE']):
16 logger = logging.getLogger(__name__)
17 logger.setLevel(LOG['ROOT_LEVEL'])
18 formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
19
20 if to_file:
21 file_handler = logging.FileHandler(os.path.join(OUT_PATH, 'trade_log.log'), mode='w')
22 file_handler.setLevel(LOG['FILE_LEVEL'])
23 file_handler.setFormatter(formatter)
24 logger.addHandler(file_handler)
25
26 stream_handler = logging.StreamHandler()
27 stream_handler.setLevel(LOG['CONSOLE_LEVEL'])
28 stream_handler.setFormatter(formatter)
29 logger.addHandler(stream_handler)
30
31 return logger
16def logging(self, message, level='INFO'):
17 print '*%s* %s' % (level, message)
40def setup_logging():
41 """ Configure our logger """
42 logger = logging.getLogger()
43
44 log_level = logging.DEBUG
45
46 logger.setLevel(log_level)
47
48 # Log format
49 formatter = logging.Formatter(
50 '%(asctime)s - %(filename)s:%(funcName)s() - %(levelname)s: %(message)s')
51
52 # Show log messages to stdout
53 stream_handler = logging.StreamHandler()
54 stream_handler.setLevel(log_level)
55 stream_handler.setFormatter(formatter)
56 logger.addHandler(stream_handler)
20def set_stream_logger(verbosity):
21 global logger
22
23 if verbosity == 0:
24 level = logging.WARNING
25 lstr = 'WARNING'
26 elif verbosity == 1:
27 level = logging.INFO
28 lstr = 'INFO'
29 elif verbosity == 2:
30 level = logging.DEBUG
31 lstr = 'DEBUG'
32 else:
33 level = logging.INFO
34 lstr = 'INFO'
35 logger.setLevel(level)
36 ch = logging.StreamHandler()
37 ch.terminator = ''
38 ch.setLevel(level)
39 ch.setFormatter(stream_formatter)
40 logger.addHandler(ch)
41 logger.info('Setting logging to %s' % lstr)

Related snippets