From 512f77b4652d6c793c21c7b6955480e6b9ada195 Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Fri, 20 Jun 2014 15:52:47 -0400 Subject: [PATCH] LP#1343578: Add support for log tagging The ability to provide instance-specific log tagging via syslog would make running multiple instances much easier to manage. So this branch does that. Signed-off-by: Mike Rylander Signed-off-by: Galen Charlton --- include/opensrf/log.h | 2 ++ src/libopensrf/log.c | 27 ++++++++++++++++++++++++++- src/libopensrf/osrf_system.c | 3 +++ src/perl/lib/OpenSRF/Utils/Logger.pm | 4 ++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/include/opensrf/log.h b/include/opensrf/log.h index 78ba808..c960afc 100644 --- a/include/opensrf/log.h +++ b/include/opensrf/log.h @@ -75,6 +75,8 @@ extern "C" { void osrfLogInit( int type, const char* appname, int maxlevel ); +void osrfLogSetLogTag( const char* logtag ); + void osrfLogSetSyslogFacility( int facility ); void osrfLogSetSyslogActFacility( int facility ); diff --git a/src/libopensrf/log.c b/src/libopensrf/log.c index 27eb6f3..1bb2747 100644 --- a/src/libopensrf/log.c +++ b/src/libopensrf/log.c @@ -22,6 +22,7 @@ static int _osrfLogActFacility = LOG_LOCAL1; static char* _osrfLogFile = NULL; /** Application name. This string will preface every log message. */ static char* _osrfLogAppname = NULL; +static char* _osrfLogTag = NULL; /** Maximum message level. Messages of higher levels will be suppressed. Default: OSRF_LOG_INFO. */ static int _osrfLogLevel = OSRF_LOG_INFO; @@ -53,6 +54,9 @@ static void _osrfLogSetXid( const char* xid ); - log type (reset to OSRF_LOG_TYPE_STDERR) */ void osrfLogCleanup( void ) { + if (_osrfLogTag) + free(_osrfLogTag); + _osrfLogTag = NULL; free(_osrfLogAppname); _osrfLogAppname = NULL; free(_osrfLogFile); @@ -278,8 +282,16 @@ void osrfLogSetActivityEnabled( int enabled ) { */ void osrfLogSetAppname( const char* appname ) { if(!appname) return; + + char buf[256]; + if(_osrfLogTag) { + snprintf(buf, sizeof(buf), "%s/%s", appname, _osrfLogTag); + } else { + snprintf(buf, sizeof(buf), "%s", appname); + } + if(_osrfLogAppname) free(_osrfLogAppname); - _osrfLogAppname = strdup(appname); + _osrfLogAppname = strdup(buf); /* if syslogging, re-open the log with the appname */ if( _osrfLogType == OSRF_LOG_TYPE_SYSLOG) { @@ -300,6 +312,19 @@ void osrfLogSetSyslogFacility( int facility ) { _osrfLogFacility = facility; } +/** + @brief Store an arbitrary program name tag for future use. + @param logtag The string to be stored. + + A log tag is a short string that is appended to the appname + we log under. This can be used to segregate logs from different + users in, for instance, rsyslogd. +*/ + +void osrfLogSetLogTag( const char* logtag ) { + if (logtag) _osrfLogTag = strdup(logtag); +} + /** @brief Store a facility number for future use for activity messages. @param facility The facility number to be stored. diff --git a/src/libopensrf/osrf_system.c b/src/libopensrf/osrf_system.c index f201a15..7a95819 100644 --- a/src/libopensrf/osrf_system.c +++ b/src/libopensrf/osrf_system.c @@ -381,6 +381,7 @@ int osrfSystemBootstrapClientResc( const char* config_file, char* unixpath = osrfConfigGetValue( NULL, "/unixpath" ); char* facility = osrfConfigGetValue( NULL, "/syslog" ); char* actlog = osrfConfigGetValue( NULL, "/actlog" ); + char* logtag = osrfConfigGetValue( NULL, "/logtag" ); /* if we're a source-client, tell the logger */ char* isclient = osrfConfigGetValue(NULL, "/client"); @@ -394,6 +395,7 @@ int osrfSystemBootstrapClientResc( const char* config_file, if(log_level) llevel = atoi(log_level); if(!strcmp(log_file, "syslog")) { + if(logtag) osrfLogSetLogTag(strdup(logtag)); osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel ); osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility)); if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog)); @@ -444,6 +446,7 @@ int osrfSystemBootstrapClientResc( const char* config_file, free(unixpath); free(facility); free(actlog); + free(logtag); return 0; } diff --git a/src/perl/lib/OpenSRF/Utils/Logger.pm b/src/perl/lib/OpenSRF/Utils/Logger.pm index f97d557..77c8593 100644 --- a/src/perl/lib/OpenSRF/Utils/Logger.pm +++ b/src/perl/lib/OpenSRF/Utils/Logger.pm @@ -35,6 +35,7 @@ my $facility; # syslog facility my $actfac; # activity log syslog facility my $actfile; # activity log file my $service = $0; # default service name +my $service_tag = ''; # default service name my $syslog_enabled = 0; # is syslog enabled? my $act_syslog_enabled = 0; # is syslog enabled? my $logfile_enabled = 1; # are we logging to a file? @@ -74,6 +75,8 @@ sub set_config { $max_log_msg_len = $config->bootstrap->loglength; } + $service_tag = $config->bootstrap->logtag; + $logfile = $config->bootstrap->logfile; if($logfile =~ /^syslog/) { $syslog_enabled = 1; @@ -159,6 +162,7 @@ sub is_act_filelog { sub set_service { my( $self, $svc ) = @_; $service = $svc; + $service .= '/' . $service_tag if (defined $service_tag); if( is_syslog() ) { closelog(); openlog($service, 0, $facility); -- 2.43.2