File: modbus_logging.py

package info (click to toggle)
pymodbus 2.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,708 kB
  • sloc: python: 17,594; makefile: 84; sh: 8
file content (46 lines) | stat: -rwxr-xr-x 2,047 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
"""
Pymodbus Logging Examples
--------------------------------------------------------------------------
"""
import logging
import logging.handlers as Handlers

if __name__ == "__main__":
    # ----------------------------------------------------------------------- #
    # This will simply send everything logged to console
    # ----------------------------------------------------------------------- #
    logging.basicConfig()
    log = logging.getLogger()
    log.setLevel(logging.DEBUG)

    # ----------------------------------------------------------------------- #
    # This will send the error messages in the specified namespace to a file.
    # The available namespaces in pymodbus are as follows:
    # ----------------------------------------------------------------------- #
    # * pymodbus.*          - The root namespace
    # * pymodbus.server.*   - all logging messages involving the modbus server
    # * pymodbus.client.*   - all logging messages involving the client
    # * pymodbus.protocol.* - all logging messages inside the protocol layer
    # ----------------------------------------------------------------------- #
    logging.basicConfig()
    log = logging.getLogger('pymodbus.server')
    log.setLevel(logging.ERROR)

    # ----------------------------------------------------------------------- #
    # This will send the error messages to the specified handlers:
    # * docs.python.org/library/logging.html
    # ----------------------------------------------------------------------- #
    log = logging.getLogger('pymodbus')
    log.setLevel(logging.ERROR)
    handlers = [
        Handlers.RotatingFileHandler("logfile", maxBytes=1024*1024),
        Handlers.SMTPHandler("mx.host.com",
                             "pymodbus@host.com",
                             ["support@host.com"],
                             "Pymodbus"),
        Handlers.SysLogHandler(facility="daemon"),
        Handlers.DatagramHandler('localhost', 12345),
    ]
    [log.addHandler(h) for h in handlers]