任何人都可以帮忙吗? 我在python中编写一个简单的脚本。我想做的是 以我的脚本将输出记录为标准格式的日志文件 apache日志格式。
我的剧本:
>>> def examine(a):
... if a == "OK":
... print True
... else:
... print False
检查(' sasasa') 的假
检查(' OK') 的真
好主意是将此函数的输出记录到* .log文件中 和apache一样。
所以代码应该继续这样:
import logging
logger = logging.getLogger('name_of_my_application')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('logFileName.log')
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info(examine('sasasa'))
问题可能是如何做出正确的formatter = logging.Formatter(...)
64.242.88.10 - - [07/Mar/2004:16:05:49 -0800] "HERE is the output of my function like: True of False" 401 12846
我需要这个,因为我将在此日志文件中应用 grok 过滤器以在ELK堆栈中显示它们
COMMONAPACHELOG %{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})" %{NUMBER:response} (?:%{NUMBER:bytes}|-)
COMBINEDAPACHELOG %{COMMONAPACHELOG} %{QS:referrer} %{QS:agent}
# Log Levels
LOGLEVEL ([Aa]lert|ALERT|[Tt]race|TRACE|[Dd]ebug|DEBUG|[Nn]otice|NOTICE|[Ii]nfo|INFO|[Ww]arn?(?:ing)?|WARN?(?:ING)?|[Ee]rr?(?:or)?|ERR?(?:OR)?|[Cc]rit?(?:ical)?|CRIT?(?:ICAL)?|[Ff]atal|FATAL|[Ss]evere|SEVERE|EMERG(?:ENCY)?|[Ee]merg(?:ency)?)