Writing a simple logger with a timestamp and counter in Jython

I am trying to write a simple logger in Jython, and I cannot find any working examples on the Internet.

I checked http://www.jython.org/docs/library/logging.html , but the code does not seem to work for me.

I just want to have a simple loop that logs a line with a timestamp and a message (log number / account) in a log file every thirty seconds. I do this on Windows using Sikuli .

Example log message:

2012-04-26 13:25:51 message number 75

Can anybody help me.

Thanks in advance,

Marwan

PS - I have never used Jython before.

+3
source share
2 answers

:

import time

LOG_CNT = 0

def log(msg):
    global LOG_CNT
    LOG_CNT += 1
    dts = time.strftime('%Y-%m-%d %H:%M:%S')
    f = open('log.txt', 'a')
    f.write('%s: msg# %d: %s\n' % (dts, LOG_CNT, msg))
    f.close()


def test():
    log('zorro')
    time.sleep(5)
    log('bubu')


test()
+5

@Michał Niklas , print python, stdout.

def log(msg):
    global LOG_CNT
    LOG_CNT += 1
    dts = time.strftime('%Y-%m-%d %H:%M:%S')
    print dts + "msg#" + LOG_CNT + msg

Sikuli .

sikli-ide.exe myProject.sikuli > stdoutfile.txt

Sikuli , , , , / sikuli.

+2

All Articles