Skip to content

Commit

Permalink
use csv for csv formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Starke committed Nov 1, 2023
1 parent a9790d1 commit 972b5bc
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lib/lokilogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import logging
from logging import handlers
import socket
import csv
from .helpers import removeNonAsciiDrop

__version__ = '0.51.0'
Expand All @@ -29,7 +30,8 @@ class LokiLogger:

no_log_file = False
log_file = "loki.log"
csv = False
use_csv = False
csv_writer = None
hostname = "NOTSET"
alerts = 0
warnings = 0
Expand All @@ -40,12 +42,12 @@ class LokiLogger:
debug = False
linesep = "\n"

def __init__(self, no_log_file, log_file, hostname, remote_host, remote_port, syslog_tcp, csv, only_relevant, debug, platform, caller, customformatter=None):
def __init__(self, no_log_file, log_file, hostname, remote_host, remote_port, syslog_tcp, use_csv, only_relevant, debug, platform, caller, customformatter=None):
self.version = __version__
self.no_log_file = no_log_file
self.log_file = log_file
self.hostname = hostname
self.csv = csv
self.use_csv = use_csv
self.only_relevant = only_relevant
self.debug = debug
self.caller = caller
Expand All @@ -57,8 +59,13 @@ def __init__(self, no_log_file, log_file, hostname, remote_host, remote_port, sy
init()

# Welcome
if not self.csv:
if not self.use_csv:
self.print_welcome()
else:
self.csv_writer = csv.DictWriter(sys.stdout,
delimiter=',',
quoting=csv.QUOTE_MINIMAL,
fieldnames=['Timestamp', 'Hostname', 'Type', 'Message'])

# Syslog server target
if remote_host:
Expand Down Expand Up @@ -117,8 +124,19 @@ def Format(self, type, message, *args):

def log_to_stdout(self, message, mes_type):

if self.csv:
print(self.Format(self.STDOUT_CSV, '{0},{1},{2},{3}', getSyslogTimestamp(), self.hostname, mes_type, message))
if self.use_csv:
if self.csv_writer:
self.csv_writer.writerow({
'Timestamp': getSyslogTimestamp(),
'Hostname': self.hostname,
'Type': mes_type,
'Message': message})
else:
print(self.Format(self.STDOUT_CSV, '{0},{1},{2},{3}',
getSyslogTimestamp(),
self.hostname,
mes_type,
message))

else:
try:
Expand Down Expand Up @@ -184,7 +202,7 @@ def log_to_file(self, message, mes_type, module):
try:
# Write to file
with codecs.open(self.log_file, "a", encoding='utf-8') as logfile:
if self.csv:
if self.use_csv:
logfile.write(self.Format(self.FILE_CSV, u"{0},{1},{2},{3},{4}{5}", getSyslogTimestamp(), self.hostname, mes_type, module, message, self.linesep))
else:
logfile.write(self.Format(self.FILE_LINE, u"{0} {1} LOKI: {2}: MODULE: {3} MESSAGE: {4}{5}", getSyslogTimestamp(), self.hostname, mes_type.title(), module, message, self.linesep))
Expand Down

0 comments on commit 972b5bc

Please sign in to comment.