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=
44 MESSAGE_ID can be given to uniquely identify the type of
47 Other parts of the message can be specified as keyword
50 Both MESSAGE and MESSAGE_ID, if present, must be strings, and
51 will be sent as UTF-8 to journal. Other arguments can be
52 bytes, in which case they will be sent as-is to journal.
54 CODE_LINE, CODE_FILE, and CODE_FUNC can be specified to
55 identify the caller. Unless at least on of the three is given,
56 values are extracted from the stack frame of the caller of
57 send(). CODE_FILE and CODE_FUNC must be strings, CODE_LINE
60 Other useful fields include PRIORITY, SYSLOG_FACILITY,
61 SYSLOG_IDENTIFIER, SYSLOG_PID.
64 args = ['MESSAGE=' + MESSAGE]
66 if MESSAGE_ID is not None:
67 args.append('MESSAGE_ID=' + MESSAGE_ID)
69 if CODE_LINE == CODE_FILE == CODE_FUNC == None:
70 CODE_FILE, CODE_LINE, CODE_FUNC = \
71 _traceback.extract_stack(limit=2)[0][:3]
72 if CODE_FILE is not None:
73 args.append('CODE_FILE=' + CODE_FILE)
74 if CODE_LINE is not None:
75 args.append('CODE_LINE={:d}'.format(CODE_LINE))
76 if CODE_FUNC is not None:
77 args.append('CODE_FUNC=' + CODE_FUNC)
79 args.extend(_make_line(key, val) for key, val in kwargs.items())
82 def stream(identifier, priority=LOG_DEBUG, level_prefix=False):
83 r"""Return a file object wrapping a stream to journal.
85 Log messages written to this file as simple newline sepearted
86 text strings are written to the journal.
88 The file will be line buffered, so messages are actually sent
89 after a newline character is written.
91 >>> stream = journal.stream('myapp')
93 <open file '<fdopen>', mode 'w' at 0x...>
94 >>> stream.write('message...\n')
96 will produce the following message in the journal:
99 SYSLOG_IDENTIFIER=myapp
102 Using the interface with print might be more convinient:
104 >>> from __future__ import print_function
105 >>> print('message...', file=stream)
107 priority is the syslog priority, one of LOG_EMERG, LOG_ALERT,
108 LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG.
110 level_prefix is a boolean. If true, kernel-style log priority
111 level prefixes (such as '<1>') are interpreted. See
112 sd-daemon(3) for more information.
115 fd = stream_fd(identifier, priority, level_prefix)
116 return _os.fdopen(fd, 'w', 1)