10 examples of 'python log to stdout and file' in Python

Every line of 'python log to stdout and file' 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
383def log(loglevel, msg):
384 """
385 Writes a message to stdout and optional logfile
386 if given loglevel is >= verbose_level()
387
388 @param msg: message to output
389 """
390 if verbose_level() >= loglevel:
391 global ARGS # pylint: disable=global-statement
392 strtime = str(time.strftime("%Y-%m-%d %H:%M:%S"))
393 # print strtime+': '+msg
394 if ARGS.logfile is not None:
395 filename = str(time.strftime(ARGS.logfile, time.localtime()))
396 logfile = open(filename, "a")
397 logfile.write(strtime+': '+msg+'\n')
398 logfile.close()
399 print(strtime+': '+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

96def _actual_log(self, string, level):
97 if self.f is None:
98 self.__open()
99 self.f.write(self.format(string, level)+"\n")
100 self.f.flush()
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)
661def logfile( message ):
662 f = codecs.open( filename, 'a', encoding='utf-8' )
663 f.write( message+"\n" )
664 f.close()
58def write(self, message, is_terminal=1, is_file=1):
59 if '\r' in message:
60 is_file = 0
61
62 if is_terminal == 1:
63 self.terminal.write(message)
64 self.terminal.flush()
65 # time.sleep(1)
66
67 if is_file == 1:
68 self.file.write(message)
69 self.file.flush()
120def OutputLog():
121 if clArgs.no_verbose:
122 return
123 f = open("check_visit_db.out","w")
124 for line in log:
125 f.write(line)
126 f.close()
8def log2file(filename): # Loggging to file
9 # set up logging to file
10 logging.basicConfig(
11 filename=os.path.join(filename + "_log.txt"),
12 level=logging.DEBUG,
13 format='[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
14 datefmt='%H:%M:%S'
15 )
16
17 # set up logging to console
18 console = logging.StreamHandler()
19 console.setLevel(logging.INFO)
20 # set a format which is simpler for console use
21 formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
22 console.setFormatter(formatter)
23 # add the handler to the root logger
24 logging.getLogger('').addHandler(console)
25
26 logger = logging.getLogger(__name__)
22def writelog(text):
23 log = open(logfile,'a')
24 message = "%s - %s: %s\n", (HOSTNAME,time.asctime(),text)
25 log.write(text)
26 log.close()
3def log(msg):
4 file(log_filename, "a").write(msg + "\n")
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

Related snippets