Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Python Logging
Me?
• 

== Chen Chun-Chia

== CCC
• MitraCloud 

>>> Python/Django

>>> Android
2
• logger, handler
• level, filter, formatter
• config
• logging flow
Python 3.4
4
>>> import logging
>>> logging.warning('haha')
WARNING:root:haha
5
>>> import logging
>>> logging.warning('haha')
WARNING:root:haha
Log level
6
>>> import logging
>>> logging.warning('haha')
WARNING:root:haha
Log message
7
>>> import logging
>>> logging.warning('haha')
WARNING:root:haha
Log level
8
>>> import logging
>>> logging.warning('haha')
WARNING:root:haha
Log message
9
>>> import logging
>>> logging.warning('haha')
WARNING:root:haha
... logger
10
>>> import logging
>>> logging.warning('haha')
WARNING:root:haha
IN
OUT
11
Logging
12
Logger Handler
13
Logger Handler
level
filter
level
filter
formatter
14
Handler
Logger Handler
level
filter
level
filter
formatter
config
Logger
15
level
16
>>> import logging
>>> logging.warning('haha')
WARNING:root:haha
Log level
17
level
level
———————
CRITICAL
ERROR
WARNING
INFO
DEBUG
50
40
30
20
10
18
level
logging.basicConfig(level=logging.ERROR)
logging.critical('critical')
logging.error('error')
logging.warning('warnin...
level
logging.basicConfig(level=logging.INFO)
logging.critical('critical')
logging.error('error')
logging.warning('warning...
level
level
———————
CRITICAL
ERROR
WARNING
INFO
DEBUG
level WARNING
21
Logger Handler
Logger Handlerfilter filter
formatter
config
level level
22
Logger
23
getLogger Logger
import logging
logging.basicConfig()
logger = logging.getLogger('mylogger')
logger.error('haha')
ERROR:my...
Logger
root
app1 app2
app1.a app1.b app2.c
25
Logger
root
app1 app2
app1.a app1.b app2.c
import logging
logging.basicConfig()
a = logging.getLogger('app1.a')
a.error('h...
Logger
root
app1 app2
app1.a app1.b app2.c
27
Handler
Logger Handler
Logger filter filter
formatter
config
level level
28
log
format
29
format
import logging
logging.basicConfig(format='*** %(message)s ***’)
logging.error('haha')
*** haha ***
30
format
1
2
3
4
5
6
7
import logging



logging.basicConfig(

format='%(filename)s:%(lineno)s %(message)s’
)

logging.error...
format
import logging
logging.basicConfig(format='*** %(message)s ***')
logging.error('haha')
*** haha ***
32
format
import logging
logging.basicConfig(format='*** %(message)s ***')
logging.error('haha')
*** haha ***
33
format
import logging
logging.basicConfig(format='*** %(message)s ***')
logging.error('haha')
*** haha ***https://docs.pyt...
Handler
Logger Handler
Logger filter filter
formatter
config
level level
35
log
36
handler
import logging



a = logging.getLogger('mylogger')



handler = logging.FileHandler('a.log')

a.addHandler(handle...
handler
import logging



a = logging.getLogger('mylogger')



h1 = logging.FileHandler('1.log')
h2 = logging.FileHandler(...
handler
StreamHandler DatagramHandler
FileHandler SysLogHandler
NullHandler NTEventLogHandler
WatchedFileHandler SMTPHandl...
Handler
Handler
Logger filter filter
formatter
config
level level
Logger
40
handler
format
41
format
a = logging.getLogger('mylogger')



h1 = logging.FileHandler('1.log')
h2 = logging.FileHandler('2.log')

h1.setFor...
Handler
Logger Handler
Logger filter filter
formatter
config
level level
43
level
handler
44
level
a = logging.getLogger('mylogger')



h1 = logging.FileHandler('1.log')
h2 = logging.FileHandler('2.log')

h1.setLeve...
Handler
Logger Handler
Logger filter filter
formatter
config
level level
46
Handler
Logger Handler
Logger filter filter
formatter
config
level level
Logger Handler level
log
47
Filter log
48
Filter
class IpFilter(logging.Filter):

def filter(self, record):

record.ip = '192.168.1.2'

return True



logging.basic...
Filter
class IpFilter(logging.Filter):

def filter(self, record):

record.ip = '192.168.1.2'

return True



logging.basic...
Filter
class IpFilter(logging.Filter):

def filter(self, record):

record.ip = '192.168.1.2'

return True



logging.basic...
Handler
Logger Handler
Logger
formatter
config
level level
filter filter
52
handler
...
53
handler
54
Handler emit
class MyHandler(logging.Handler):


def __init__(self, widget):

super(MyHandler, self).__init__()

self._wid...
QPlainTextEdit
class MyHandler(logging.Handler):


def __init__(self, widget):

super(MyHandler, self).__init__()

self._w...
57
app = QApplication(sys.argv)

a = logging.getLogger('a')



widget = QPlainTextEdit()

h = MyHandler(widget)

a.addHand...
Handler
Logger Handler
Logger
formatter
config
level level
filter filter
58
logging
...
59
dict
60
...
f = logging.Formatter(‘*** %(name)s:%(message)s ***’)



my_handler = logging.FileHandler('a.log')

my_handler.setLeve...
Dict Logging
f = logging.Formatter(‘*** %(name)s:%(message)s ***’)



my_handler = logging.FileHandler('a.log')

my_handle...
Dict Logging
f = logging.Formatter(‘*** %(name)s:%(message)s ***’)



my_handler = logging.FileHandler('a.log')

my_handle...
...
from logging.config import dictConfig

dictConfig(config)


a = logging.getLogger('my_logger')



a.error('error')

a....
65
...
f = logging.Formatter(‘*** %(name)s:%(message)s ***’)



my_handler = logging.FileHandler('a.log')

my_handler.setLeve...
logging
f = logging.Formatter(‘*** %(name)s:%(message)s ***’)



my_handler = logging.FileHandler('a.log')

my_handler.set...
logging
f = logging.Formatter(‘*** %(name)s:%(message)s ***’)



my_handler = logging.FileHandler('a.log')

my_handler.set...
...
from logging.config import fileConfig

fileConfig('my_logging.cfg')


a = logging.getLogger('my_logger')



a.error('e...
70
%(xxx)s
defaults
71
[formatters]

keys=my_formatter



[handlers]

keys=my_handler



[loggers]

keys=root,my_logger
[formatter_my_formatter]
...
[formatters]

keys=my_formatter



[handlers]

keys=my_handler



[loggers]

keys=root,my_logger
[formatter_my_formatter]
...
[formatters]

keys=my_formatter



[handlers]

keys=my_handler



[loggers]

keys=root,my_logger
[formatter_my_formatter]
...
...
75
basicConfig
76
basicConfig
StreamHandler + default Formatter
root logger
level, format, filename, …
77
basicConfig
logging.basicConfig(level=logging.ERROR)
level
logging.basicConfig(format='*** %(message)s ***')
format
logging...
basicConfig
logging.basicConfig(
level=logging.ERROR,
format='*** %(message)s ***',
filename='a.log'
)
level format filename...
Handler
Logger Handler
level
filter
level
filter
formatter
config
Logger
80
logging
log
81
LogRecord
logger.info('xxx') logger
level
logger filter
record
YES
YES
NO
NO
bye
bye
handler
YES
bye bye
NO
parent logger
...
YES
handler
YES logger
propagate
YES
NO
bye
YES
NO
bye
handler
level
handler filter
record
83
https://docs.python.org/3.5/howto/logging.html#logging-flow
84
Basic Tutorial & Advanced Tutorial
https://docs.python.org/3.5/howto/logging.html
Logging Cookbook
https://docs.python.org...
Thank You
Nächste SlideShare
Wird geladen in …5
×

20151024 Taichung.py 講一些 Python Logging

524 Aufrufe

Veröffentlicht am

介紹 Python Logging Framework 的元件、設定與使用
- logger, handler
- level, filter, format
- config (dict, file)
- logging flow

Veröffentlicht in: Software
  • Als Erste(r) kommentieren

20151024 Taichung.py 講一些 Python Logging

  1. 1. Python Logging
  2. 2. Me? • 
 == Chen Chun-Chia
 == CCC • MitraCloud 
 >>> Python/Django
 >>> Android 2
  3. 3. • logger, handler • level, filter, formatter • config • logging flow
  4. 4. Python 3.4 4
  5. 5. >>> import logging >>> logging.warning('haha') WARNING:root:haha 5
  6. 6. >>> import logging >>> logging.warning('haha') WARNING:root:haha Log level 6
  7. 7. >>> import logging >>> logging.warning('haha') WARNING:root:haha Log message 7
  8. 8. >>> import logging >>> logging.warning('haha') WARNING:root:haha Log level 8
  9. 9. >>> import logging >>> logging.warning('haha') WARNING:root:haha Log message 9
  10. 10. >>> import logging >>> logging.warning('haha') WARNING:root:haha ... logger 10
  11. 11. >>> import logging >>> logging.warning('haha') WARNING:root:haha IN OUT 11
  12. 12. Logging 12
  13. 13. Logger Handler 13
  14. 14. Logger Handler level filter level filter formatter 14
  15. 15. Handler Logger Handler level filter level filter formatter config Logger 15
  16. 16. level 16
  17. 17. >>> import logging >>> logging.warning('haha') WARNING:root:haha Log level 17
  18. 18. level level ——————— CRITICAL ERROR WARNING INFO DEBUG 50 40 30 20 10 18
  19. 19. level logging.basicConfig(level=logging.ERROR) logging.critical('critical') logging.error('error') logging.warning('warning') logging.info('info') logging.debug('debug') CRITICAL:root:critical ERROR:root:error level ——————— CRITICAL ERROR WARNING INFO DEBUG 19
  20. 20. level logging.basicConfig(level=logging.INFO) logging.critical('critical') logging.error('error') logging.warning('warning') logging.info('info') logging.debug('debug') CRITICAL:root:critical ERROR:root:error WARNING:root:warning INFO:root:info level ——————— CRITICAL ERROR WARNING INFO DEBUG 20
  21. 21. level level ——————— CRITICAL ERROR WARNING INFO DEBUG level WARNING 21
  22. 22. Logger Handler Logger Handlerfilter filter formatter config level level 22
  23. 23. Logger 23
  24. 24. getLogger Logger import logging logging.basicConfig() logger = logging.getLogger('mylogger') logger.error('haha') ERROR:mylogger:haha 24
  25. 25. Logger root app1 app2 app1.a app1.b app2.c 25
  26. 26. Logger root app1 app2 app1.a app1.b app2.c import logging logging.basicConfig() a = logging.getLogger('app1.a') a.error('haha') ERROR:app1.a:haha 26
  27. 27. Logger root app1 app2 app1.a app1.b app2.c 27
  28. 28. Handler Logger Handler Logger filter filter formatter config level level 28
  29. 29. log format 29
  30. 30. format import logging logging.basicConfig(format='*** %(message)s ***’) logging.error('haha') *** haha *** 30
  31. 31. format 1 2 3 4 5 6 7 import logging
 
 logging.basicConfig(
 format='%(filename)s:%(lineno)s %(message)s’ )
 logging.error('haha') a.py:7 haha a.py 31
  32. 32. format import logging logging.basicConfig(format='*** %(message)s ***') logging.error('haha') *** haha *** 32
  33. 33. format import logging logging.basicConfig(format='*** %(message)s ***') logging.error('haha') *** haha *** 33
  34. 34. format import logging logging.basicConfig(format='*** %(message)s ***') logging.error('haha') *** haha ***https://docs.python.org/3.5/library/logging.html#logrecord-attributes 34
  35. 35. Handler Logger Handler Logger filter filter formatter config level level 35
  36. 36. log 36
  37. 37. handler import logging
 
 a = logging.getLogger('mylogger')
 
 handler = logging.FileHandler('a.log')
 a.addHandler(handler)
 
 a.error('hahaha') hahaha a.log 37
  38. 38. handler import logging
 
 a = logging.getLogger('mylogger')
 
 h1 = logging.FileHandler('1.log') h2 = logging.FileHandler('2.log')
 a.addHandler(h1)
 a.addHandler(h2)
 
 a.error('hahaha') hahaha 1.log hahaha 2.log 38
  39. 39. handler StreamHandler DatagramHandler FileHandler SysLogHandler NullHandler NTEventLogHandler WatchedFileHandler SMTPHandler BaseRotatingHandler MemoryHandler RotatingFileHandler HTTPHandler TimedRotatingFileHandler QueueHandler SocketHandler https://docs.python.org/3.5/library/logging.handlers.html 39
  40. 40. Handler Handler Logger filter filter formatter config level level Logger 40
  41. 41. handler format 41
  42. 42. format a = logging.getLogger('mylogger')
 
 h1 = logging.FileHandler('1.log') h2 = logging.FileHandler('2.log')
 h1.setFormatter( logging.Formatter('*** %(message)s ***'))
 h2.setFormatter( logging.Formatter('%(levelname)s:%(message)s'))
 a.addHandler(h1)
 a.addHandler(h2)
 
 a.error('haha') *** haha ***1.log ERROR:haha2.log 42
  43. 43. Handler Logger Handler Logger filter filter formatter config level level 43
  44. 44. level handler 44
  45. 45. level a = logging.getLogger('mylogger')
 
 h1 = logging.FileHandler('1.log') h2 = logging.FileHandler('2.log')
 h1.setLevel(logging.ERROR)
 h2.setLevel(logging.WARNING)
 
 a.addHandler(h1)
 a.addHandler(h2)
 
 a.error('error')
 a.warning('warning') error1.log error warning 2.log 45
  46. 46. Handler Logger Handler Logger filter filter formatter config level level 46
  47. 47. Handler Logger Handler Logger filter filter formatter config level level Logger Handler level log 47
  48. 48. Filter log 48
  49. 49. Filter class IpFilter(logging.Filter):
 def filter(self, record):
 record.ip = '192.168.1.2'
 return True
 
 logging.basicConfig(format='[%(ip)s] %(message)s')
 a = logging.getLogger('mylogger')
 
 f = IpFilter()
 a.addFilter(f)
 
 a.error('hahaha') 49 record: LogRecord log log LogRecord
  50. 50. Filter class IpFilter(logging.Filter):
 def filter(self, record):
 record.ip = '192.168.1.2'
 return True
 
 logging.basicConfig(format='[%(ip)s] %(message)s')
 a = logging.getLogger('mylogger')
 
 f = IpFilter()
 a.addFilter(f)
 
 a.error('hahaha') 50
  51. 51. Filter class IpFilter(logging.Filter):
 def filter(self, record):
 record.ip = '192.168.1.2'
 return True
 
 logging.basicConfig(format='[%(ip)s] %(message)s')
 a = logging.getLogger('mylogger')
 
 f = IpFilter()
 a.addFilter(f)
 
 a.error('hahaha') [192.168.1.2] hahaha 51
  52. 52. Handler Logger Handler Logger formatter config level level filter filter 52
  53. 53. handler ... 53
  54. 54. handler 54
  55. 55. Handler emit class MyHandler(logging.Handler): 
 def __init__(self, widget):
 super(MyHandler, self).__init__()
 self._widget = widget
 
 def emit(self, record):
 msg = self.format(record) 
 cursor = self._widget.textCursor()
 cursor.insertText(msg)
 cursor.insertText('n') 55
  56. 56. QPlainTextEdit class MyHandler(logging.Handler): 
 def __init__(self, widget):
 super(MyHandler, self).__init__()
 self._widget = widget
 
 def emit(self, record):
 msg = self.format(record) 
 cursor = self._widget.textCursor()
 cursor.insertText(msg)
 cursor.insertText('n') PyQt 56
  57. 57. 57 app = QApplication(sys.argv)
 a = logging.getLogger('a')
 
 widget = QPlainTextEdit()
 h = MyHandler(widget)
 a.addHandler(h)
 widget.show()
 
 a.error('hihi')
 a.warning('lol')
 
 sys.exit(app.exec_())
  58. 58. Handler Logger Handler Logger formatter config level level filter filter 58
  59. 59. logging ... 59
  60. 60. dict 60
  61. 61. ... f = logging.Formatter(‘*** %(name)s:%(message)s ***’)
 
 my_handler = logging.FileHandler('a.log')
 my_handler.setLevel(logging.DEBUG)
 my_handler.setFormatter(f)
 
 a = logging.getLogger('my_logger')
 a.setLevel(logging.INFO)
 a.addHandler(my_handler)
 
 a.error('error')
 a.info('info') 61
  62. 62. Dict Logging f = logging.Formatter(‘*** %(name)s:%(message)s ***’)
 
 my_handler = logging.FileHandler('a.log')
 my_handler.setLevel(logging.DEBUG)
 my_handler.setFormatter(f)
 
 a = logging.getLogger('my_logger')
 a.setLevel(logging.INFO)
 a.addHandler(my_handler)
 
 a.error('error')
 a.info('info') config = {
 'version': 1,
 'formatters': {
 'my_formatter': {
 'format': '*** %(name)s:%(message)s ***'
 },
 },
 'handlers': {
 'my_handler': {
 'level': 'DEBUG',
 'class': 'logging.FileHandler',
 'filename': 'a.log',
 'formatter': 'my_formatter',
 }
 },
 'loggers': {
 'my_logger': {
 'level': 'INFO',
 'handlers': ['my_handler'],
 }
 },
 } 62
  63. 63. Dict Logging f = logging.Formatter(‘*** %(name)s:%(message)s ***’)
 
 my_handler = logging.FileHandler('a.log')
 my_handler.setLevel(logging.DEBUG)
 my_handler.setFormatter(f)
 
 a = logging.getLogger('my_logger')
 a.setLevel(logging.INFO)
 a.addHandler(my_handler)
 
 a.error('error')
 a.info('info') config = {
 'version': 1,
 'formatters': {
 'my_formatter': {
 'format': '*** %(name)s:%(message)s ***'
 },
 },
 'handlers': {
 'my_handler': {
 'level': 'DEBUG',
 'class': 'logging.FileHandler',
 'filename': 'a.log',
 'formatter': 'my_formatter',
 }
 },
 'loggers': {
 'my_logger': {
 'level': 'INFO',
 'handlers': ['my_handler'],
 }
 },
 } from logging.config import dictConfig
 dictConfig(config) 63
  64. 64. ... from logging.config import dictConfig
 dictConfig(config) 
 a = logging.getLogger('my_logger')
 
 a.error('error')
 a.info('info') 64
  65. 65. 65
  66. 66. ... f = logging.Formatter(‘*** %(name)s:%(message)s ***’)
 
 my_handler = logging.FileHandler('a.log')
 my_handler.setLevel(logging.DEBUG)
 my_handler.setFormatter(f)
 
 a = logging.getLogger('my_logger')
 a.setLevel(logging.INFO)
 a.addHandler(my_handler)
 
 a.error('error')
 a.info('info') 66
  67. 67. logging f = logging.Formatter(‘*** %(name)s:%(message)s ***’)
 
 my_handler = logging.FileHandler('a.log')
 my_handler.setLevel(logging.DEBUG)
 my_handler.setFormatter(f)
 
 a = logging.getLogger('my_logger')
 a.setLevel(logging.INFO)
 a.addHandler(my_handler)
 
 a.error('error')
 a.info('info') [formatters]
 keys=my_formatter
 
 [handlers]
 keys=my_handler
 
 [loggers]
 keys=root,my_logger [formatter_my_formatter]
 format=*** %(name)s:%(message)s ***
 
 [handler_my_handler]
 level=DEBUG
 class=FileHandler
 formatter=my_formatter
 args=('a.log',)
 
 [logger_my_logger]
 level=INFO
 handlers=my_handler
 qualname=my_logger
 
 [logger_root]
 handlers= my_logging.cfg 67
  68. 68. logging f = logging.Formatter(‘*** %(name)s:%(message)s ***’)
 
 my_handler = logging.FileHandler('a.log')
 my_handler.setLevel(logging.DEBUG)
 my_handler.setFormatter(f)
 
 a = logging.getLogger('my_logger')
 a.setLevel(logging.INFO)
 a.addHandler(my_handler)
 
 a.error('error')
 a.info('info') [formatters]
 keys=my_formatter
 
 [handlers]
 keys=my_handler
 
 [loggers]
 keys=root,my_logger [formatter_my_formatter]
 format=*** %(name)s:%(message)s ***
 
 [handler_my_handler]
 level=DEBUG
 class=FileHandler
 formatter=my_formatter
 args=('a.log',)
 
 [logger_my_logger]
 level=INFO
 handlers=my_handler
 qualname=my_logger
 
 [logger_root]
 handlers= my_logging.cfg from logging.config import fileConfig
 fileConfig('my_logging.cfg') 68
  69. 69. ... from logging.config import fileConfig
 fileConfig('my_logging.cfg') 
 a = logging.getLogger('my_logger')
 
 a.error('error')
 a.info('info') 69
  70. 70. 70
  71. 71. %(xxx)s defaults 71
  72. 72. [formatters]
 keys=my_formatter
 
 [handlers]
 keys=my_handler
 
 [loggers]
 keys=root,my_logger [formatter_my_formatter]
 format=*** %(name)s:%(message)s ***
 
 [handler_my_handler]
 level=DEBUG
 class=FileHandler
 formatter=my_formatter
 args=('a.log',)
 
 [logger_my_logger]
 level=INFO
 handlers=my_handler
 qualname=my_logger
 
 [logger_root]
 handlers= 72
  73. 73. [formatters]
 keys=my_formatter
 
 [handlers]
 keys=my_handler
 
 [loggers]
 keys=root,my_logger [formatter_my_formatter]
 format=*** %(name)s:%(message)s ***
 
 [handler_my_handler]
 level=DEBUG
 class=FileHandler
 formatter=my_formatter
 args=('%(my_log_file)s',)
 
 [logger_my_logger]
 level=INFO
 handlers=my_handler
 qualname=my_logger
 
 [logger_root]
 handlers= %(xxx)s 73
  74. 74. [formatters]
 keys=my_formatter
 
 [handlers]
 keys=my_handler
 
 [loggers]
 keys=root,my_logger [formatter_my_formatter]
 format=*** %(name)s:%(message)s ***
 
 [handler_my_handler]
 level=DEBUG
 class=FileHandler
 formatter=my_formatter
 args=('%(my_log_file)s',)
 
 [logger_my_logger]
 level=INFO
 handlers=my_handler
 qualname=my_logger
 
 [logger_root]
 handlers= fileConfig( 'my_logging.cfg', defaults={'my_log_file': 'a.log'} ) defaults 74
  75. 75. ... 75
  76. 76. basicConfig 76
  77. 77. basicConfig StreamHandler + default Formatter root logger level, format, filename, … 77
  78. 78. basicConfig logging.basicConfig(level=logging.ERROR) level logging.basicConfig(format='*** %(message)s ***') format logging.basicConfig(filename='a.log') filename 78
  79. 79. basicConfig logging.basicConfig( level=logging.ERROR, format='*** %(message)s ***', filename='a.log' ) level format filename 79
  80. 80. Handler Logger Handler level filter level filter formatter config Logger 80
  81. 81. logging log 81
  82. 82. LogRecord logger.info('xxx') logger level logger filter record YES YES NO NO bye bye handler YES bye bye NO parent logger YES NO logger propagate parent logger 82
  83. 83. YES handler YES logger propagate YES NO bye YES NO bye handler level handler filter record 83
  84. 84. https://docs.python.org/3.5/howto/logging.html#logging-flow 84
  85. 85. Basic Tutorial & Advanced Tutorial https://docs.python.org/3.5/howto/logging.html Logging Cookbook https://docs.python.org/3.5/howto/logging-cookbook.html Python Library Document https://docs.python.org/3.5/library/logging.html 85
  86. 86. Thank You

×