Skip to content

Instantly share code, notes, and snippets.

@clarkzjw
Last active January 19, 2017 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarkzjw/7e90db7e7e131b4a9e2d79d0d9307e46 to your computer and use it in GitHub Desktop.
Save clarkzjw/7e90db7e7e131b4a9e2d79d0d9307e46 to your computer and use it in GitHub Desktop.
LogStash
import logging
import time
from logging.handlers import RotatingFileHandler
# ----------------------------------------------------------------------
def create_rotating_log(path):
"""
Creates a rotating log
"""
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.INFO)
# add a rotating handler
handler = RotatingFileHandler(path, maxBytes=100,
backupCount=5)
logger.addHandler(handler)
for i in range(1, 100):
logger.info("This is test log line %s" % i)
print("This is test log line %s" % i)
time.sleep(1)
# ----------------------------------------------------------------------
if __name__ == "__main__":
log_file = "test.log"
create_rotating_log(log_file)
input {
file{
# Path 根据应用的实际情况来填
path => ["/home/clarkzjw/Tomcat/python/test.log.*", "/home/clarkzjw/Tomcat/python/test.log"]
# discover_interval尽量设置小一点,单位是秒,作用是发现新的Log文件的周期
discover_interval => 1
start_position => "beginning"
sincedb_path => "/home/clarkzjw/Tomcat/python/sincedb"
}
# 如果以Filebeat作为输入
# beats {
# port => 5044
# }
}
output {
# 这里把读取到的Log输出到stdout,也可对接ELK等方案
stdout{ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment