def test_journal_stream(): # This will fail when running in a bare chroot without /run/systemd/journal/stdout with skip_oserror(errno.ENOENT): stream = journal.stream('test_journal.py') res = stream.write('message...\n') assert res in (11, None) # Python2 returns None print('printed message...', file=stream)
def pre_sos_setup(self): # ensure that case-insensitive matching of FQDNs and shortnames work from systemd import journal from socket import gethostname host = gethostname() short = host.split('.')[0] sosfd = journal.stream('sos-testing') sosfd.write( "This is a test line from sos clean testing. The hostname %s " "should not appear, nor should %s in an obfuscated archive. The " "shortnames of %s and %s should also not appear." % (host.lower(), host.upper(), short.lower(), short.upper()))
def log(*args, **kwargs): if dCheck(config, 'log'): if not '_logstream' in runtime: runtime['_logstream'] = journal.stream('slimIMAP') logdata = ' '.join([str(x) for x in args]) if dCheck(config, 'resolve'): ## TODO: Resolve internal UID's etc to something readable pass if not 'level' in kwargs or kwargs['level'] >= config['log_level']: log_row = {'level' : (kwargs['level'] if 'level' in kwargs else None), 'message' : logdata} log_row.update(kwargs) print(json.dumps(log_row), file=runtime['_logstream'])
def pre_sos_setup(self): # write 20MB at a time to side-step rate/size limiting on some distros # write over 100MB to ensure we will actually size limit inside sos, # allowing for any compression or de-dupe systemd does from systemd import journal sosfd = journal.stream('sos-testing') rsize = 10 * 1048576 for i in range(6): # generate 10MB, write it, then write it in reverse. # Spend less time generating new strings rand = ''.join( random.choice(ascii_uppercase + digits) for _ in range(rsize)) sosfd.write(rand + '\n') # sleep to avoid burst rate-limiting sleep(10) sosfd.write(rand[::-1] + '\n')
def log_journal(*messages): """Log messages to systemd journal.""" stream = journal.stream(basename) print(*messages, file=stream)
def info(message): """Print info messages if requested.""" stream = journal.stream('fix_lte') print('INFO:', message, file=stream) if DEBUG or INFO: print('INFO:', message)
def error(message): """Print error messages if requested.""" stream = journal.stream('fix_lte') print('ERROR:', message, file=stream) if not QUIET: print('ERROR:', message)
#!/usr/bin/python3 # See https://www.freedesktop.org/software/systemd/python-systemd/journal.html from systemd import journal journal.send('Hello world') journal.send('Hello, again, world', FIELD2='Greetings!') stream = journal.stream('myapp', priority=3) res = stream.write('message...\n', )
def log(message,level=6,app="checkIP.py"): log = journal.stream(app,level) log.write(message)