You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.2 KiB
83 lines
2.2 KiB
![]()
9 years ago
|
// +build !windows
|
||
|
|
||
|
/*
|
||
![]()
9 years ago
|
* Minio Cloud Storage, (C) 2015, 2016 Minio, Inc.
|
||
![]()
9 years ago
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
![]()
8 years ago
|
package cmd
|
||
![]()
9 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log/syslog"
|
||
|
|
||
|
"github.com/Sirupsen/logrus"
|
||
|
)
|
||
|
|
||
![]()
9 years ago
|
type syslogLogger struct {
|
||
|
Enable bool `json:"enable"`
|
||
|
Addr string `json:"address"`
|
||
|
Level string `json:"level"`
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// syslogHook to send logs via syslog.
|
||
|
type syslogHook struct {
|
||
|
writer *syslog.Writer
|
||
|
syslogNetwork string
|
||
|
syslogRaddr string
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// enableSyslogLogger - enable logger at raddr.
|
||
|
func enableSyslogLogger(raddr string) {
|
||
![]()
8 years ago
|
syslogHook, err := newSyslog("udp", raddr, syslog.LOG_ERR, "MINIO")
|
||
![]()
8 years ago
|
fatalIf(err, "Unable to initialize syslog logger.")
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
log.Hooks.Add(syslogHook) // Add syslog hook.
|
||
|
log.Formatter = &logrus.JSONFormatter{} // JSON formatted log.
|
||
![]()
8 years ago
|
log.Level = logrus.ErrorLevel // Minimum log level.
|
||
![]()
9 years ago
|
}
|
||
|
|
||
![]()
9 years ago
|
// newSyslog - Creates a hook to be added to an instance of logger.
|
||
![]()
9 years ago
|
func newSyslog(network, raddr string, priority syslog.Priority, tag string) (*syslogHook, error) {
|
||
![]()
8 years ago
|
w, err := syslog.Dial(network, raddr, priority, tag)
|
||
|
return &syslogHook{w, network, raddr}, err
|
||
![]()
9 years ago
|
}
|
||
|
|
||
|
// Fire - fire the log event
|
||
|
func (hook *syslogHook) Fire(entry *logrus.Entry) error {
|
||
![]()
8 years ago
|
line, err := entry.String()
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Unable to read entry, %v", err)
|
||
![]()
9 years ago
|
}
|
||
|
switch entry.Level {
|
||
|
case logrus.PanicLevel:
|
||
|
return hook.writer.Crit(line)
|
||
|
case logrus.FatalLevel:
|
||
|
return hook.writer.Crit(line)
|
||
|
case logrus.ErrorLevel:
|
||
|
return hook.writer.Err(line)
|
||
|
default:
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Levels -
|
||
|
func (hook *syslogHook) Levels() []logrus.Level {
|
||
|
return []logrus.Level{
|
||
|
logrus.PanicLevel,
|
||
|
logrus.FatalLevel,
|
||
|
logrus.ErrorLevel,
|
||
|
}
|
||
|
}
|