github.com/atc0005/elbow@v0.8.8/internal/logging/logging_unix.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  // Copyright 2020 Adam Chalkley
     5  //
     6  // https://github.com/atc0005/elbow
     7  //
     8  // Licensed under the Apache License, Version 2.0 (the "License");
     9  // you may not use this file except in compliance with the License.
    10  // You may obtain a copy of the License at
    11  //
    12  //     https://www.apache.org/licenses/LICENSE-2.0
    13  //
    14  // Unless required by applicable law or agreed to in writing, software
    15  // distributed under the License is distributed on an "AS IS" BASIS,
    16  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17  // See the License for the specific language governing permissions and
    18  // limitations under the License.
    19  
    20  package logging
    21  
    22  import (
    23  	"fmt"
    24  
    25  	// Use `log` if we are going to override the default `log`, otherwise
    26  	// import without an "override" if we want to use the `logrus` name.
    27  	// https://godoc.org/github.com/sirupsen/logrus
    28  	"github.com/sirupsen/logrus"
    29  
    30  	// Official examples use either `lSyslog` or `logrus_syslog`
    31  	// https://godoc.org/github.com/sirupsen/logrus/hooks/syslog
    32  	lSyslog "github.com/sirupsen/logrus/hooks/syslog"
    33  
    34  	"log/syslog"
    35  )
    36  
    37  // EnableSyslogLogging attempts to enable local syslog logging for non-Windows
    38  // systems. For Windows systems the attempt is skipped.
    39  func EnableSyslogLogging(logger *logrus.Logger, logBuffer *LogBuffer, logLevel string) error {
    40  
    41  	// Use roughly the same logging level as specified for the general logger
    42  	// https://golang.org/pkg/log/syslog/#Priority
    43  	// https://en.wikipedia.org/wiki/Syslog#Severity_level
    44  	// https://pubs.opengroup.org/onlinepubs/009695399/functions/syslog.html
    45  	var syslogLogLevel syslog.Priority
    46  
    47  	switch logLevel {
    48  	case LogLevelEmergency, LogLevelPanic:
    49  		syslogLogLevel = syslog.LOG_EMERG
    50  	case LogLevelAlert, LogLevelFatal:
    51  		syslogLogLevel = syslog.LOG_ALERT
    52  	case LogLevelCritical:
    53  		syslogLogLevel = syslog.LOG_CRIT
    54  	case LogLevelError:
    55  		syslogLogLevel = syslog.LOG_ERR
    56  	case LogLevelWarn:
    57  		syslogLogLevel = syslog.LOG_WARNING
    58  	case LogLevelNotice:
    59  		syslogLogLevel = syslog.LOG_NOTICE
    60  	case LogLevelInfo:
    61  		syslogLogLevel = syslog.LOG_INFO
    62  	case LogLevelDebug:
    63  		syslogLogLevel = syslog.LOG_DEBUG
    64  	case LogLevelTrace:
    65  		syslogLogLevel = syslog.LOG_DEBUG
    66  	default:
    67  		return fmt.Errorf("invalid syslog log level: %q", logLevel)
    68  	}
    69  
    70  	logBuffer.Add(LogRecord{
    71  		Level:   logrus.DebugLevel,
    72  		Message: fmt.Sprintf("syslog log level set to %v", syslogLogLevel),
    73  		Fields:  logrus.Fields{"log_level": logLevel},
    74  	})
    75  
    76  	// Attempt to connect to local syslog
    77  	//
    78  	// FIXME: Is this setting a "cap" on the level of log messages that flow
    79  	// through or is this an indication of the type of messages that ALL
    80  	// logging calls will produce? I'm assuming we're setting a limiter here
    81  	hook, err := lSyslog.NewSyslogHook("", "", syslogLogLevel, "")
    82  
    83  	if err == nil {
    84  
    85  		logBuffer.Add(LogRecord{
    86  			Level:   logrus.InfoLevel,
    87  			Message: "Connected to syslog socket",
    88  		})
    89  
    90  		logger.AddHook(hook)
    91  
    92  		logBuffer.Add(LogRecord{
    93  			Level:   logrus.DebugLevel,
    94  			Message: "Finished using AddHook() to enable syslog logging",
    95  		})
    96  
    97  	} else {
    98  		return fmt.Errorf("unable to connect to syslog socket: %w", err)
    99  	}
   100  
   101  	return nil
   102  
   103  }