From 17f98499e86055efaf643d815f8adce08c3800af Mon Sep 17 00:00:00 2001 From: unknown <tsmith@ramayana.hindu.god> Date: Fri, 27 Jul 2007 17:20:43 -0600 Subject: [PATCH] Bug #29992: syslog error logging does not flush Don't use syslog by default; user will have to request it explicitly with the --syslog option. Use "sed -u" to get unbuffered output from sed, if it's supported. Otherwise, don't use sed at all - don't strip the timestamp from mysqld messages. Also, add new --syslog-tag=FOO option, which adds "-FOO" to the tag used when logging messages to syslog (i.e., mysqld-FOO or mysqld_safe-FOO) Also, explicitly mention where log messages are going, so user can more easily find them. Also, check if 'logger' is in the PATH, and log to the error log file if it can't be found. --- scripts/mysqld_safe.sh | 116 ++++++++++++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 25 deletions(-) diff --git a/scripts/mysqld_safe.sh b/scripts/mysqld_safe.sh index 597bf38a51..2a592a6fbf 100644 --- a/scripts/mysqld_safe.sh +++ b/scripts/mysqld_safe.sh @@ -14,12 +14,17 @@ KILL_MYSQLD=1; MYSQLD= niceness=0 -# Default on, unless --log-error is specified (and before options are parsed) -syslog=2 +# Initial logging status: error log is not open, and not using syslog +logging=init +want_syslog=0 +syslog_tag= user=@MYSQLD_USER@ pid_file= err_log= +syslog_tag_mysqld=mysqld +syslog_tag_mysqld_safe=mysqld_safe + trap '' 1 2 3 15 # we shouldn't let anyone kill us umask 007 @@ -46,7 +51,8 @@ Usage: $0 [OPTIONS] --nice=NICE Set the scheduling priority of mysqld --skip-kill-mysqld Don't try to kill stray mysqld processes --syslog Log messages to syslog with 'logger' - --skip-syslog Log messages to error log + --skip-syslog Log messages to error log (default) + --syslog-tag=TAG Pass -t "mysqld-TAG" to 'logger' All other options are passed to the mysqld program. @@ -54,18 +60,46 @@ EOF exit 1 } +my_which () +{ + save_ifs="${IFS-UNSET}" + IFS=: + for file + do + for dir in $PATH + do + if [ -f "$dir/$file" ] + then + echo "$dir/$file" + continue 2 + fi + done + return 1 # Failure, didn't find file in path + done + if [ "$save_ifs" = UNSET ] + then + unset IFS + else + IFS="$save_ifs" + fi + return 0 # Success +} + log_generic () { priority="$1" shift msg="`date +'%y%m%d %H:%M:%S'` mysqld_safe $*" echo "$msg" - if [ $syslog -eq 0 ] - then - echo "$msg" >> "$err_log" - else - logger -i -t mysqld_safe -p "$priority" "$*" - fi + case $logging in + init) ;; # Just echo the message, don't save it anywhere + file) echo "$msg" >> "$err_log" ;; + syslog) logger -t "$syslog_tag_mysqld_safe" -p "$priority" "$*" ;; + *) + echo "Internal program error (non-fatal):" \ + " unknown logging method '$logging'" >&2 + ;; + esac } log_error () { @@ -78,15 +112,23 @@ log_notice () { eval_log_error () { cmd="$1" - if [ $syslog -eq 0 ] - then - cmd="$cmd >> "`shell_quote_string "$err_log"`" 2>&1" - else - # mysqld often (not always) prefixes messages on stdout with a - # timestamp in the form of '%y%m%d %H:%M:%S '; this is redundant - # when logging via syslog, so strip it - cmd="$cmd 2>&1 | sed -e 's/^[0-9]\{6\} [0-9:]\{8\} *//' | logger -i -t mysqld -p daemon.error" - fi + case $logging in + file) cmd="$cmd >> "`shell_quote_string "$err_log"`" 2>&1" ;; + syslog) + # mysqld often prefixes its messages with a timestamp, which is + # redundant when logging to syslog (which adds its own timestamp) + # However, we don't strip the timestamp with sed here, because + # sed buffers output (only GNU sed supports a -u (unbuffered) option) + # which means that messages may not get sent to syslog until the + # mysqld process quits. + cmd="$cmd 2>&1 | logger -t '$syslog_tag_mysqld' -p daemon.error" + ;; + *) + echo "Internal program error (non-fatal):" \ + " unknown logging method '$logging'" >&2 + ;; + esac + #echo "Running mysqld: [$cmd]" eval "$cmd" } @@ -138,8 +180,9 @@ parse_arguments() { --nice=*) niceness="$val" ;; --open-files-limit=*) open_files="$val" ;; --skip-kill-mysqld*) KILL_MYSQLD=0 ;; - --syslog) syslog=1 ;; - --skip-syslog) syslog=0 ;; + --syslog) want_syslog=1 ;; + --skip-syslog) want_syslog=0 ;; + --syslog-tag=*) syslog_tag="$val" ;; --timezone=*) TZ="$val"; export TZ; ;; --help) usage ;; @@ -252,7 +295,19 @@ parse_arguments `$print_defaults $defaults --loose-verbose mysqld_safe safe_mysq parse_arguments PICK-ARGS-FROM-ARGV "$@" # Determine what logging facility to use -if [ -n "$err_log" -o $syslog -eq 0 ] + +# Ensure that 'logger' exists, if it's requested +if [ $want_syslog -eq 1 ] +then + my_which logger > /dev/null 2>&1 + if [ $? -ne 0 ] + then + log_error "--syslog requested, but no 'logger' program found." + want_syslog=0 + fi +fi + +if [ -n "$err_log" -o $want_syslog -eq 0 ] then if [ -n "$err_log" ] then @@ -279,14 +334,25 @@ then append_arg_to_args "--log-error=$err_log" - if [ $syslog -eq 1 ] + if [ $want_syslog -eq 1 ] then # User explicitly asked for syslog, so warn that it isn't used - log_error "Can't log to error log and syslog at the same time. Remove all --log-error configuration options for --syslog to take effect. Logging to '$err_log'." + log_error "Can't log to error log and syslog at the same time. Remove all --log-error configuration options for --syslog to take effect." fi - # Don't use syslog since syslog and error log don't mix well - syslog=0 + # Log to err_log file + log_notice "Logging to '$err_log'." + logging=file +else + if [ -n "$syslog_tag" ] + then + # Sanitize the syslog tag + syslog_tag=`echo "$syslog_tag" | sed -e 's/[^a-zA-Z0-9_-]/_/g'` + syslog_tag_mysqld_safe="${syslog_tag_mysqld_safe}-$syslog_tag" + syslog_tag_mysqld="${syslog_tag_mysqld}-$syslog_tag" + fi + log_notice "Logging to syslog." + logging=syslog fi USER_OPTION="" -- 2.30.9