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.
76 lines
1.9 KiB
76 lines
1.9 KiB
9 years ago
|
/*
|
||
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"
|
||
|
"os"
|
||
|
|
||
|
"github.com/Sirupsen/logrus"
|
||
|
)
|
||
|
|
||
9 years ago
|
type fileLogger struct {
|
||
|
Enable bool `json:"enable"`
|
||
|
Filename string `json:"fileName"`
|
||
|
Level string `json:"level"`
|
||
|
}
|
||
|
|
||
9 years ago
|
type localFile struct {
|
||
|
*os.File
|
||
|
}
|
||
|
|
||
9 years ago
|
func enableFileLogger() {
|
||
|
flogger := serverConfig.GetFileLogger()
|
||
9 years ago
|
if !flogger.Enable || flogger.Filename == "" {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
// Creates the named file with mode 0666, honors system umask.
|
||
|
file, err := os.OpenFile(flogger.Filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
||
9 years ago
|
fatalIf(err, "Unable to open log file.")
|
||
9 years ago
|
|
||
9 years ago
|
// Add a local file hook.
|
||
|
log.Hooks.Add(&localFile{file})
|
||
9 years ago
|
|
||
9 years ago
|
lvl, err := logrus.ParseLevel(flogger.Level)
|
||
9 years ago
|
fatalIf(err, "Unknown log level found in the config file.")
|
||
9 years ago
|
|
||
9 years ago
|
// Set default JSON formatter.
|
||
|
log.Formatter = new(logrus.JSONFormatter)
|
||
9 years ago
|
log.Level = lvl // Minimum log level.
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Fire fires the file logger hook and logs to the file.
|
||
9 years ago
|
func (l *localFile) Fire(entry *logrus.Entry) error {
|
||
|
line, err := entry.String()
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Unable to read entry, %v", err)
|
||
|
}
|
||
|
l.File.Write([]byte(line + "\n"))
|
||
|
l.File.Sync()
|
||
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// Levels - indicate log levels supported.
|
||
9 years ago
|
func (l *localFile) Levels() []logrus.Level {
|
||
|
return []logrus.Level{
|
||
|
logrus.PanicLevel,
|
||
|
logrus.FatalLevel,
|
||
|
logrus.ErrorLevel,
|
||
|
}
|
||
|
}
|