Repository navigation
Expand file tree
/
Copy path036_python_logger.py
More file actions
76 lines (59 loc) · 2.47 KB
/
Copy path036_python_logger.py
File metadata and controls
76 lines (59 loc) · 2.47 KB
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
#========================================================================================
# TOPIC: PYTHON - SENDING EMAIL
#========================================================================================
# NOTES: * PYTHON provides a standard logging module to log messages
# * LOG to Console
# * LOG to File
#
#========================================================================================
#
# FILE-NAME : 036_python_logger.py
# DEPENDANT-FILES : N/A
#
# AUTHOR : tinitiate.com / Venkata Bhattaram
# (c) 2014
#
# DESC : PYTHON LOGGING
#
#========================================================================================
import sys, logging
##########################
# 1) LOG to CONSOLE
##########################
# Create a LOGGING Object
tinitiateLOG = logging.getLogger('tinitiate')
tinitiateLOG.setLevel(logging.DEBUG)
print(tinitiateLOG.getEffectiveLevel())
# Create a Streamhandler for console
tinLOG = logging.StreamHandler( sys.__stdout__ )
# Print to console level, Add the stream handler to Console
tinLOG.setLevel(logging.DEBUG)
tinitiateLOG.addHandler(tinLOG)
# Use the functions [debug, info, warn, error] of the logging object
tinitiateLOG.debug('This is a debug message')
tinitiateLOG.info('This is a info message')
tinitiateLOG.warn('This is a warning message')
tinitiateLOG.error('This is an error message')
tinitiateLOG.critical('This is a critical message')
##########################
# 2) LOG to FILE
##########################
# Create a File Handler
LogFile = logging.FileHandler('c:\\tinitiate\\tinPython.log')
# Set Log level of ERROR, ONLY ERROR or HIGHER are logged to FILE
LogFile.setLevel(logging.ERROR)
# Add the handler to the logging object
tinitiateLOG.addHandler(LogFile)
# Run Log messages
tinitiateLOG.debug('This is a debug message')
tinitiateLOG.info('This is a info message')
tinitiateLOG.warn('This is a warning message')
# ONLY THE FOLLOEING MESSAGES ARE LOGGED TO FILE
tinitiateLOG.error('This is an error message')
tinitiateLOG.critical('This is a critical message')
#========================================================================================
# END OF CODE
#========================================================================================
#TAGS: PYTHON - LOGGING LOG DEBUG LOG ERROR LOG WARNING LOG ERROR LOG CRITICAL
# LOG TO CONSOLE LOG TO FILE
#========================================================================================