小编Mar*_*628的帖子

python:测试将日志发送到 Syslog 服务器

请帮助我,如何将python脚本日志发送到syslog服务器(syslog-ng产品),我已经尝试过以下方法..它有两种方法。一个是“SysLogHandler”,另一个是“SocketHandler”

import logging
import logging.handlers
import socket

my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)

handler = logging.handlers.SysLogHandler(address=('10.10.11.11', 611), socktype=socket.SOCK_STREAM)
#handler = logging.handlers.SocketHandler('10.10.11.11', 611)

my_logger.addHandler(handler)

my_logger.debug('this is debug')
my_logger.critical('this is critical')
Run Code Online (Sandbox Code Playgroud)

结果:SysLogHandler

[ansible@localhost ~]$ python test.py
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    handler = logging.handlers.SysLogHandler(address=('10.10.11.11', 611), socktype=socket.SOCK_STREAM)
  File "/usr/lib64/python3.6/logging/handlers.py", line 847, in __init__
    raise err
  File "/usr/lib64/python3.6/logging/handlers.py", line 840, in __init__
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused #THIS IS OK for me since server unreachable.
Error in atexit._run_exitfuncs:
Traceback …
Run Code Online (Sandbox Code Playgroud)

python syslog python-logging

5
推荐指数
1
解决办法
5269
查看次数

python3:timedrotatingfilehandler 日志轮换问题与多个脚本相同的日志文件

请帮助解决来自多个进程的 python3 日志记录到同一个日志文件的问题。我有 dameon 主脚本,它在后台运行,每 15 秒调用一些其他 python 脚本,并且每个 python 脚本都使用相同的 TimedRotatingFileHandler 属性编写,并且所有日志都写入相同的日志文件。

一切正常,除了日志轮换。

lib/
??? __init__.py
??? FIRST.py
??? SECOND.py
??? THIRD.py
main_daemon.py
Run Code Online (Sandbox Code Playgroud)

主 python 守护程序文件看起来像

t1 = t2 = t3 = Thread()
my_thread = MYthreads(t1, t2, t3)
################# Daemon Class #################
class Daemon(Daemon):
        def run(self):
             while True:
                my_thread.start_my_class()
                time.sleep(15)

ProcessManager = Daemon('daemon.pid')
ProcessManager.start()
Run Code Online (Sandbox Code Playgroud)

lib/__init__.py 文件看起来不错

class MYthreads:
    def __init__(self, t1, t2, t3):
        self.t1 = t1
        self.t2 = t2
        self.t3 = t3

    def start_my_class(self):
            for file in List_files(path1):
                self.t1 …
Run Code Online (Sandbox Code Playgroud)

python logging python-3.x

5
推荐指数
1
解决办法
284
查看次数

Python:将 csv 转换为 dict - 使用标题作为键

Python: 3.x

你好。我有下面的 csv 文件,其中有标题和行。行数可能因文件而异。我正在尝试将此 csv 转换为 dict 格式,并且第一行的数据正在重复。

"cdrRecordType","globalCallID_callManagerId","globalCallID_callId"
1,3,9294899
1,3,9294933
Run Code Online (Sandbox Code Playgroud)

Code:

parserd_list = []
output_dict = {}
with open("files\\CUCMdummy.csv") as myfile:
    firstline = True
    for line in myfile:
        if firstline:
            mykeys = ''.join(line.split()).split(',')
            firstline = False
        else:
            values = ''.join(line.split()).split(',')
            for n in range(len(mykeys)):
                output_dict[mykeys[n].rstrip('"').lstrip('"')] = values[n].rstrip('"').lstrip('"')
                print(output_dict)
                parserd_list.append(output_dict)
#print(parserd_list)
Run Code Online (Sandbox Code Playgroud)

(通常我的 csv 列数超过 20,但我提供了一个示例文件。)

(我使用 rstrip/lstrip 来去掉双引号。)

Output getting:

{'cdrRecordType': '1'}
{'cdrRecordType': '1', 'globalCallID_callManagerId': '3'}
{'cdrRecordType': '1', 'globalCallID_callManagerId': '3', 'globalCallID_callId': '9294899'}
{'cdrRecordType': '1', 'globalCallID_callManagerId': '3', 'globalCallID_callId': '9294899'}
{'cdrRecordType': …
Run Code Online (Sandbox Code Playgroud)

python

4
推荐指数
1
解决办法
2852
查看次数

标签 统计

python ×3

logging ×1

python-3.x ×1

python-logging ×1

syslog ×1