LP#1343578: Add support for log tagging
authorMike Rylander <mrylander@gmail.com>
Fri, 20 Jun 2014 19:52:47 +0000 (15:52 -0400)
committerGalen Charlton <gmc@esilibrary.com>
Wed, 20 Aug 2014 20:53:47 +0000 (13:53 -0700)
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 <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>

include/opensrf/log.h
src/libopensrf/log.c
src/libopensrf/osrf_system.c
src/perl/lib/OpenSRF/Utils/Logger.pm

index 78ba808..c960afc 100644 (file)
@@ -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 );
index 27eb6f3..1bb2747 100644 (file)
@@ -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) {
@@ -301,6 +313,19 @@ void osrfLogSetSyslogFacility( int 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.
 
index f201a15..7a95819 100644 (file)
@@ -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;
        }
 
index f97d557..77c8593 100644 (file)
@@ -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);