aboutsummaryrefslogtreecommitdiff
blob: 5a87a923d3a9e0f8446cc8512bf57d21c57106c6 (plain)
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
#!/usr/bin/env python

import datetime
import glob
import os
import re
import shutil

from grs.Constants import CONST
from grs.Rotator import Rotator

class Log(Rotator):

    def __init__(self, logfile = CONST.LOGFILE):
        self.logfile = logfile
        os.makedirs(os.path.dirname(self.logfile), exist_ok=True)
        open(self.logfile, 'a').close()


    def log(self, msg, stamped = True):
        if stamped:
            current_time = datetime.datetime.now(datetime.timezone.utc)
            unix_timestamp = current_time.timestamp()
            msg = '[%f] %s' % (unix_timestamp, msg)
        with open(self.logfile, 'a') as f:
            f.write('%s\n' % msg)


    def rotate_logs(self, upper_limit = 20):
        self.full_rotate(self.logfile, upper_limit=upper_limit)
        open('%s' % self.logfile, 'a').close()