-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
145 lines (127 loc) · 4.55 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2020 Pradyumna Kaushik
//
// 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.
package logger
import (
"fmt"
"github.com/pkg/errors"
"github.com/pradykaushik/task-ranker/logger/topic"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"strings"
"time"
)
const (
// Using RFC3339Nano as the timestamp format for logs as prometheus scrape interval can be in milliseconds.
timestampFormat = time.RFC3339Nano
// Prefix of the name of the log file to store task ranker logs.
// This will be suffixed with the timestamp, associated with creating the file, to obtain the log filename.
taskRankerLogFilePrefix = "task_ranker_logs"
// Prefix of the name of the log file to store task ranking results.
// This will be suffixed with the timestamp, associated with creating the file, to obtain the log filename.
taskRankingResultsLogFilePrefix = "task_ranking_results"
// Giving everyone read and write permissions to the log files.
logFilePermissions = 0666
)
// instantiating a Logger to be used. This instance is configured and maintained locally.
var log = logrus.New()
var taskRankerLogFile *os.File
var taskRankingResultsLogFile *os.File
// createTaskRankerLogFile creates the log file to which task ranker logs are persisted.
func createTaskRankerLogFile(now time.Time) error {
var err error
filename := fmt.Sprintf("%s_%v.log", taskRankerLogFilePrefix, now.UnixNano())
taskRankerLogFile, err = os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, logFilePermissions)
if err != nil {
err = errors.Wrap(err, "failed to create task ranker operations log file")
}
return err
}
// createTaskRankingResultsLogFile creates the log file to which task ranking results are persisted.
func createTaskRankingResultsLogFile(now time.Time) error {
var err error
filename := fmt.Sprintf("%s_%v.log", taskRankingResultsLogFilePrefix, now.UnixNano())
taskRankingResultsLogFile, err = os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, logFilePermissions)
if err != nil {
err = errors.Wrap(err, "failed to create task ranker log file")
}
return err
}
// Configure the logger. To be prevented task ranker logs from mixing with the logs of the application
// that is using it, logging to the console is disabled and instead hooks that redirect logs to corresponding
// log files are attached to the logger.
func Configure() error {
// Disabling log to stdout.
log.SetOutput(ioutil.Discard)
// Setting highest log level.
log.SetLevel(logrus.InfoLevel)
// Creating the log files.
now := time.Now()
var err error
if err = createTaskRankerLogFile(now); err != nil {
return err
}
if err = createTaskRankingResultsLogFile(now); err != nil {
return err
}
// Instantiate the hooks.
jsonFormatter := &logrus.JSONFormatter{
DisableHTMLEscape: true,
TimestampFormat: timestampFormat,
}
textFormatter := &logrus.TextFormatter{
DisableColors: true,
FullTimestamp: true,
TimestampFormat: timestampFormat,
}
// Reading in list of topics that have been disabled.
disabledTopics := make(map[topic.Topic]struct{})
if value := os.Getenv(loggingDisablerEnvVar); value != "" {
for _, value := range strings.Split(value, delimiter) {
if t := topic.FromString(value); t.IsValid() {
disabledTopics[t] = struct{}{}
}
}
}
log.AddHook(newWriterHook(textFormatter, taskRankerLogFile, disabledTopics,
topic.Stage, topic.Query, topic.QueryResult))
log.AddHook(newWriterHook(jsonFormatter, taskRankingResultsLogFile, disabledTopics,
topic.TaskRankingStrategy, topic.TaskRankingResult))
return nil
}
func Done() error {
var err error
if taskRankerLogFile != nil {
err = taskRankerLogFile.Close()
if err != nil {
err = errors.Wrap(err, "failed to close task ranker log file")
}
}
if taskRankingResultsLogFile != nil {
err = taskRankingResultsLogFile.Close()
if err != nil {
err = errors.Wrap(err, "failed to close tank ranking results log file")
}
}
return err
}
// Aliasing logrus functions.
var WithField = log.WithField
var WithFields = log.WithFields
var Info = log.Info
var Infof = log.Infof
var Error = log.Error
var Errorf = log.Errorf
var Warn = log.Warn
var Warnf = log.Warnf