1 # -*- Mode: python; indent-tabs-mode: nil -*- */
3 # This file is part of systemd.
5 # Copyright 2012 David Strauss
7 # systemd is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation; either version 2.1 of the License, or
10 # (at your option) any later version.
12 # systemd is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 import traceback as _traceback
22 from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
23 LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG)
24 from ._journal import sendv, stream_fd
26 def _make_line(field, value):
27 if isinstance(value, bytes):
28 return field.encode('utf-8') + b'=' + value
30 return field + '=' + value
32 def send(MESSAGE, MESSAGE_ID=None,
33 CODE_FILE=None, CODE_LINE=None, CODE_FUNC=None,
35 r"""Send a message to journald.
37 >>> journal.send('Hello world')
38 >>> journal.send('Hello, again, world', FIELD2='Greetings!')
39 >>> journal.send('Binary message', BINARY=b'\xde\xad\xbe\xef')
41 Value of the MESSAGE argument will be used for the MESSAGE= field.
43 MESSAGE_ID can be given to uniquely identify the type of message.
45 Other parts of the message can be specified as keyword arguments.
47 Both MESSAGE and MESSAGE_ID, if present, must be strings, and will
48 be sent as UTF-8 to journal. Other arguments can be bytes, in
49 which case they will be sent as-is to journal.
51 CODE_LINE, CODE_FILE, and CODE_FUNC can be specified to identify
52 the caller. Unless at least on of the three is given, values are
53 extracted from the stack frame of the caller of send(). CODE_FILE
54 and CODE_FUNC must be strings, CODE_LINE must be an integer.
56 Other useful fields include PRIORITY, SYSLOG_FACILITY,
57 SYSLOG_IDENTIFIER, SYSLOG_PID.
60 args = ['MESSAGE=' + MESSAGE]
62 if MESSAGE_ID is not None:
63 args.append('MESSAGE_ID=' + MESSAGE_ID)
65 if CODE_LINE == CODE_FILE == CODE_FUNC == None:
66 CODE_FILE, CODE_LINE, CODE_FUNC = \
67 _traceback.extract_stack(limit=2)[0][:3]
68 if CODE_FILE is not None:
69 args.append('CODE_FILE=' + CODE_FILE)
70 if CODE_LINE is not None:
71 args.append('CODE_LINE={:d}'.format(CODE_LINE))
72 if CODE_FUNC is not None:
73 args.append('CODE_FUNC=' + CODE_FUNC)
75 args.extend(_make_line(key, val) for key, val in kwargs.items())
78 def stream(identifier, priority=LOG_DEBUG, level_prefix=False):
79 r"""Return a file object wrapping a stream to journal.
81 Log messages written to this file as simple newline sepearted
82 text strings are written to the journal.
84 The file will be line buffered, so messages are actually sent
85 after a newline character is written.
87 >>> stream = journal.stream('myapp')
89 <open file '<fdopen>', mode 'w' at 0x...>
90 >>> stream.write('message...\n')
92 will produce the following message in the journal:
95 SYSLOG_IDENTIFIER=myapp
98 Using the interface with print might be more convinient:
100 >>> from __future__ import print_function
101 >>> print('message...', file=stream)
103 priority is the syslog priority, one of LOG_EMERG, LOG_ALERT,
104 LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG.
106 level_prefix is a boolean. If true, kernel-style log priority
107 level prefixes (such as '<1>') are interpreted. See sd-daemon(3)
108 for more information.
111 fd = stream_fd(identifier, priority, level_prefix)
112 return _os.fdopen(fd, 'w', 1)