1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

Merge pull request #13084 from ddstreet/log_time

log: add support for prefixing console log messages with current timestamp
This commit is contained in:
Lennart Poettering 2020-03-30 17:57:34 +02:00 committed by GitHub
commit 01262d0d9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 128 additions and 32 deletions

View File

@ -141,6 +141,7 @@
<varlistentry>
<term><varname>$SYSTEMD_LOG_TARGET</varname></term>
<term><varname>$SYSTEMD_LOG_LEVEL</varname></term>
<term><varname>$SYSTEMD_LOG_TIME</varname></term>
<term><varname>$SYSTEMD_LOG_COLOR</varname></term>
<term><varname>$SYSTEMD_LOG_LOCATION</varname></term>

View File

@ -63,10 +63,11 @@
<variablelist class='config-directives'>
<varlistentry>
<term><varname>LogLevel=</varname></term>
<term><varname>LogTarget=</varname></term>
<term><varname>LogColor=</varname></term>
<term><varname>LogLevel=</varname></term>
<term><varname>LogLocation=</varname></term>
<term><varname>LogTarget=</varname></term>
<term><varname>LogTime=</varname></term>
<term><varname>DumpCore=yes</varname></term>
<term><varname>CrashChangeVT=no</varname></term>
<term><varname>CrashShell=no</varname></term>

View File

@ -606,6 +606,13 @@
<title>Environment</title>
<variablelist class='environment-variables'>
<varlistentry>
<term><varname>$SYSTEMD_LOG_COLOR</varname></term>
<listitem><para>Controls whether systemd highlights important
log messages. This can be overridden with
<option>--log-color</option>.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>$SYSTEMD_LOG_LEVEL</varname></term>
<listitem><para>systemd reads the log level from this
@ -613,6 +620,13 @@
<option>--log-level=</option>.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>$SYSTEMD_LOG_LOCATION</varname></term>
<listitem><para>Controls whether systemd prints the code
location along with log messages. This can be overridden with
<option>--log-location</option>.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>$SYSTEMD_LOG_TARGET</varname></term>
<listitem><para>systemd reads the log target from this
@ -621,17 +635,10 @@
</varlistentry>
<varlistentry>
<term><varname>$SYSTEMD_LOG_COLOR</varname></term>
<listitem><para>Controls whether systemd highlights important
log messages. This can be overridden with
<option>--log-color=</option>.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>$SYSTEMD_LOG_LOCATION</varname></term>
<listitem><para>Controls whether systemd prints the code
location along with log messages. This can be overridden with
<option>--log-location=</option>.</para></listitem>
<term><varname>$SYSTEMD_LOG_TIME</varname></term>
<listitem><para>Controls whether systemd prefixes log
messages with the current time. This can be overridden with
<option>--log-time=</option>.</para></listitem>
</varlistentry>
<varlistentry>
@ -829,18 +836,21 @@
</varlistentry>
<varlistentry>
<term><varname>systemd.log_target=</varname></term>
<term><varname>systemd.log_level=</varname></term>
<term><varname>systemd.log_location=</varname></term>
<term><varname>systemd.log_color</varname></term>
<term><varname>systemd.log_level=</varname></term>
<term><varname>systemd.log_location</varname></term>
<term><varname>systemd.log_target=</varname></term>
<term><varname>systemd.log_time</varname></term>
<listitem><para>Controls log output, with the same effect as the
<varname>$SYSTEMD_LOG_TARGET</varname>,
<varname>$SYSTEMD_LOG_COLOR</varname>,
<varname>$SYSTEMD_LOG_LEVEL</varname>,
<varname>$SYSTEMD_LOG_LOCATION</varname>,
<varname>$SYSTEMD_LOG_COLOR</varname> environment variables described above.
<varname>systemd.log_color</varname> can be specified without an argument,
with the same effect as a positive boolean.</para></listitem>
<varname>$SYSTEMD_LOG_TARGET</varname>,
<varname>$SYSTEMD_LOG_TIME</varname>, environment variables described above.
<varname>systemd.log_color</varname>, <varname>systemd.log_location</varname>, and
<varname>systemd.log_time</varname> can be specified without an argument, with the
same effect as a positive boolean.</para></listitem>
</varlistentry>
<varlistentry>
@ -1135,9 +1145,10 @@
</varlistentry>
<varlistentry>
<term><option>--log-target=</option></term>
<term><option>--log-color</option></term>
<listitem><para>Set log target. See <varname>systemd.log_target</varname> above.</para></listitem>
<listitem><para>Highlight important log messages. See <varname>systemd.log_color</varname> above.
</para></listitem>
</varlistentry>
<varlistentry>
@ -1146,13 +1157,6 @@
<listitem><para>Set log level. See <varname>systemd.log_level</varname> above.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--log-color</option></term>
<listitem><para>Highlight important log messages. See <varname>systemd.log_color</varname> above.
</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--log-location</option></term>
@ -1160,6 +1164,19 @@
above.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--log-target=</option></term>
<listitem><para>Set log target. See <varname>systemd.log_target</varname> above.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--log-time=</option></term>
<listitem><para>Prefix messages with timestamp. See <varname>systemd.log_time</varname> above.
</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--machine-id=</option></term>

View File

@ -51,6 +51,7 @@ static bool syslog_is_stream = false;
static bool show_color = false;
static bool show_location = false;
static bool show_time = false;
static bool upgrade_syslog_to_journal = false;
static bool always_reopen_console = false;
@ -332,8 +333,10 @@ static int write_to_console(
const char *func,
const char *buffer) {
char location[256], prefix[1 + DECIMAL_STR_MAX(int) + 2];
struct iovec iovec[6] = {};
char location[256],
header_time[FORMAT_TIMESTAMP_MAX],
prefix[1 + DECIMAL_STR_MAX(int) + 2];
struct iovec iovec[8] = {};
const char *on = NULL, *off = NULL;
size_t n = 0;
@ -345,6 +348,13 @@ static int write_to_console(
iovec[n++] = IOVEC_MAKE_STRING(prefix);
}
if (show_time) {
if (format_timestamp(header_time, sizeof(header_time), now(CLOCK_REALTIME))) {
iovec[n++] = IOVEC_MAKE_STRING(header_time);
iovec[n++] = IOVEC_MAKE_STRING(" ");
}
}
if (show_color)
get_log_colors(LOG_PRI(level), &on, &off, NULL);
@ -1099,6 +1109,12 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
if (log_show_location_from_string(value ?: "1") < 0)
log_warning("Failed to parse log location setting '%s'. Ignoring.", value);
} else if (proc_cmdline_key_streq(key, "systemd.log_time")) {
if (log_show_time_from_string(value ?: "1") < 0)
log_warning("Failed to parse log time setting '%s'. Ignoring.", value);
}
return 0;
@ -1130,6 +1146,10 @@ void log_parse_environment_realm(LogRealm realm) {
e = getenv("SYSTEMD_LOG_LOCATION");
if (e && log_show_location_from_string(e) < 0)
log_warning("Failed to parse log location '%s'. Ignoring.", e);
e = getenv("SYSTEMD_LOG_TIME");
if (e && log_show_time_from_string(e) < 0)
log_warning("Failed to parse log time '%s'. Ignoring.", e);
}
LogTarget log_get_target(void) {
@ -1156,6 +1176,14 @@ bool log_get_show_location(void) {
return show_location;
}
void log_show_time(bool b) {
show_time = b;
}
bool log_get_show_time(void) {
return show_time;
}
int log_show_color_from_string(const char *e) {
int t;
@ -1178,6 +1206,17 @@ int log_show_location_from_string(const char *e) {
return 0;
}
int log_show_time_from_string(const char *e) {
int t;
t = parse_boolean(e);
if (t < 0)
return t;
log_show_time(t);
return 0;
}
bool log_on_console(void) {
if (IN_SET(log_target, LOG_TARGET_CONSOLE,
LOG_TARGET_CONSOLE_PREFIXED))

View File

@ -59,9 +59,12 @@ void log_show_color(bool b);
bool log_get_show_color(void) _pure_;
void log_show_location(bool b);
bool log_get_show_location(void) _pure_;
void log_show_time(bool b);
bool log_get_show_time(void) _pure_;
int log_show_color_from_string(const char *e);
int log_show_location_from_string(const char *e);
int log_show_time_from_string(const char *e);
LogTarget log_get_target(void) _pure_;
int log_get_max_level_realm(LogRealm realm) _pure_;

View File

@ -542,8 +542,9 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
DEFINE_SETTER(config_parse_level2, log_set_max_level_from_string, "log level");
DEFINE_SETTER(config_parse_target, log_set_target_from_string, "target");
DEFINE_SETTER(config_parse_color, log_show_color_from_string, "color" );
DEFINE_SETTER(config_parse_color, log_show_color_from_string, "color");
DEFINE_SETTER(config_parse_location, log_show_location_from_string, "location");
DEFINE_SETTER(config_parse_time, log_show_time_from_string, "time");
static int config_parse_default_timeout_abort(
const char *unit,
@ -571,6 +572,7 @@ static int parse_config_file(void) {
{ "Manager", "LogTarget", config_parse_target, 0, NULL },
{ "Manager", "LogColor", config_parse_color, 0, NULL },
{ "Manager", "LogLocation", config_parse_location, 0, NULL },
{ "Manager", "LogTime", config_parse_time, 0, NULL },
{ "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core },
{ "Manager", "CrashChVT", /* legacy */ config_parse_crash_chvt, 0, &arg_crash_chvt },
{ "Manager", "CrashChangeVT", config_parse_crash_chvt, 0, &arg_crash_chvt },
@ -718,6 +720,7 @@ static int parse_argv(int argc, char *argv[]) {
ARG_LOG_TARGET,
ARG_LOG_COLOR,
ARG_LOG_LOCATION,
ARG_LOG_TIME,
ARG_UNIT,
ARG_SYSTEM,
ARG_USER,
@ -745,6 +748,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "log-target", required_argument, NULL, ARG_LOG_TARGET },
{ "log-color", optional_argument, NULL, ARG_LOG_COLOR },
{ "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
{ "log-time", optional_argument, NULL, ARG_LOG_TIME },
{ "unit", required_argument, NULL, ARG_UNIT },
{ "system", no_argument, NULL, ARG_SYSTEM },
{ "user", no_argument, NULL, ARG_USER },
@ -818,6 +822,18 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_LOG_TIME:
if (optarg) {
r = log_show_time_from_string(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse log time setting \"%s\": %m",
optarg);
} else
log_show_time(true);
break;
case ARG_DEFAULT_STD_OUTPUT:
r = exec_output_from_string(optarg);
if (r < 0)
@ -1037,6 +1053,7 @@ static int help(void) {
" --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
" --log-color[=BOOL] Highlight important log messages\n"
" --log-location[=BOOL] Include code location in log messages\n"
" --log-time[=BOOL] Prefix log messages with current time\n"
" --default-standard-output= Set default standard output for services\n"
" --default-standard-error= Set default standard error output for services\n"
"\nSee the %s for details.\n"
@ -1419,6 +1436,9 @@ static int become_shutdown(
if (log_get_show_location())
command_line[pos++] = "--log-location";
if (log_get_show_time())
command_line[pos++] = "--log-time";
if (streq(shutdown_verb, "exit")) {
command_line[pos++] = "--exit-code";
command_line[pos++] = exit_code;

View File

@ -16,6 +16,7 @@
#LogTarget=journal-or-kmsg
#LogColor=yes
#LogLocation=no
#LogTime=no
#DumpCore=yes
#ShowStatus=yes
#CrashChangeVT=no

View File

@ -15,6 +15,7 @@
#LogTarget=console
#LogColor=yes
#LogLocation=no
#LogTime=no
#SystemCallArchitectures=
#TimerSlackNSec=
#StatusUnitFormat=@STATUS_UNIT_FORMAT_DEFAULT@

View File

@ -51,6 +51,7 @@ static int parse_argv(int argc, char *argv[]) {
ARG_LOG_TARGET,
ARG_LOG_COLOR,
ARG_LOG_LOCATION,
ARG_LOG_TIME,
ARG_EXIT_CODE,
ARG_TIMEOUT,
};
@ -60,6 +61,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "log-target", required_argument, NULL, ARG_LOG_TARGET },
{ "log-color", optional_argument, NULL, ARG_LOG_COLOR },
{ "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
{ "log-time", optional_argument, NULL, ARG_LOG_TIME },
{ "exit-code", required_argument, NULL, ARG_EXIT_CODE },
{ "timeout", required_argument, NULL, ARG_TIMEOUT },
{}
@ -110,6 +112,17 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_LOG_TIME:
if (optarg) {
r = log_show_time_from_string(optarg);
if (r < 0)
log_error_errno(r, "Failed to parse log time setting %s, ignoring: %m", optarg);
} else
log_show_time(true);
break;
case ARG_EXIT_CODE:
r = safe_atou8(optarg, &arg_exit_code);
if (r < 0)