Skip to content

Commit a1167f8

Browse files
nnthnnhuebnerr
authored andcommitted
(1) allow the logger backend to be implemented externally, (2) making logger.Backend.Write more generic (#52)
1 parent 90a9cc4 commit a1167f8

File tree

4 files changed

+105
-16
lines changed

4 files changed

+105
-16
lines changed

logger/filelogger.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ package logger
3232
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3333
// THE SOFTWARE.
3434

35-
import "os"
35+
import (
36+
"fmt"
37+
"os"
38+
"time"
39+
)
3640

3741
type FileLogger struct {
3842
fp *os.File
@@ -48,11 +52,13 @@ func NewFileLogger(filename string) (*FileLogger, error) {
4852
return &FileLogger{fp: file}, nil
4953
}
5054

51-
func (l *FileLogger) write(logStr string) {
52-
l.fp.Write([]byte(logStr))
55+
func (l *FileLogger) Write(prefix string, name string, msg string) {
56+
l.fp.Write([]byte(time.Now().Format(time.StampMicro)))
57+
l.fp.Write([]byte(fmt.Sprintf(" %s %s: ", prefix, name)))
58+
l.fp.Write([]byte(msg + "\n"))
5359
}
5460

55-
func (l *FileLogger) close() {
61+
func (l *FileLogger) Close() {
5662
if l.fp != nil {
5763
l.fp.Close()
5864
}

logger/logger.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
"fmt"
3737
"os"
3838
"runtime"
39-
"time"
4039
)
4140

4241
// LogLevel is the enum type for the log levels.
@@ -54,12 +53,12 @@ const (
5453

5554
// Backend defines the public interface that must be implemented by all logger backends
5655
type Backend interface {
57-
write(logStr string)
58-
close()
56+
Write(prefix string, name string, msg string)
57+
Close()
5958
}
6059

6160
var (
62-
prefixes = []string{"TRACE", "DEBUG", "INFO ", "WARN ", "ERROR", "FATAL"}
61+
prefixes = []string{"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"}
6362
level = WARN
6463
logger Backend = &STDIOLogger{}
6564
)
@@ -69,9 +68,7 @@ type Logger struct {
6968
}
7069

7170
func (l *Logger) print(level LogLevel, format string, args ...interface{}) {
72-
logger.write(time.Now().Format(time.StampMicro))
73-
logger.write(fmt.Sprintf(" %s %s: ", prefixes[level], l.name))
74-
logger.write(fmt.Sprintf(format+"\n", args...))
71+
logger.Write(prefixes[level], l.name, fmt.Sprintf(format, args...))
7572
}
7673

7774
func New(name string) *Logger {
@@ -133,7 +130,7 @@ func SetLogLevel(newLevel LogLevel) {
133130

134131
func SetLogger(newLogger Backend) {
135132
if logger != newLogger {
136-
logger.close()
133+
logger.Close()
137134
logger = newLogger
138135
}
139136
}

logger/stdiologger.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ package logger
3232
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3333
// THE SOFTWARE.
3434

35-
import "os"
35+
import (
36+
"fmt"
37+
"os"
38+
"time"
39+
)
3640

3741
type STDIOLogger struct {
3842
}
3943

40-
func (l *STDIOLogger) write(logStr string) {
41-
os.Stdout.Write([]byte(logStr))
44+
func (l *STDIOLogger) Write(prefix string, name string, msg string) {
45+
os.Stdout.Write([]byte(time.Now().Format(time.StampMicro)))
46+
os.Stdout.Write([]byte(fmt.Sprintf(" %s %s: ", prefix, name)))
47+
os.Stdout.Write([]byte(msg + "\n"))
4248
}
4349

44-
func (l *STDIOLogger) close() {
50+
func (l *STDIOLogger) Close() {
4551
}

logger_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package vertigo
2+
3+
// Copyright (c) 2019 Micro Focus or one of its affiliates.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
// Permission is hereby granted, free of charge, to any person obtaining a copy
18+
// of this software and associated documentation files (the "Software"), to deal
19+
// in the Software without restriction, including without limitation the rights
20+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21+
// copies of the Software, and to permit persons to whom the Software is
22+
// furnished to do so, subject to the following conditions:
23+
//
24+
// The above copyright notice and this permission notice shall be included in
25+
// all copies or substantial portions of the Software.
26+
//
27+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33+
// THE SOFTWARE.
34+
35+
import (
36+
"testing"
37+
38+
"github.com/vertica/vertica-sql-go/logger"
39+
)
40+
41+
type LogMessage struct {
42+
Prefix string
43+
Name string
44+
Msg string
45+
}
46+
type MyLogger struct {
47+
LastMessage *LogMessage
48+
}
49+
func (l *MyLogger) Write(prefix string, name string, msg string) {
50+
l.LastMessage = &LogMessage{Prefix: prefix, Name: name, Msg: msg}
51+
}
52+
func (l MyLogger) Close() {}
53+
54+
func TestExternalLogging(t *testing.T) {
55+
ml := &MyLogger{}
56+
57+
logger.SetLogger(ml)
58+
logger.SetLogLevel(logger.TRACE)
59+
60+
log := logger.New("TestExternalLogging")
61+
62+
log.Debug("Basic logging message here")
63+
assertEqual(t, ml.LastMessage.Prefix, "DEBUG")
64+
assertEqual(t, ml.LastMessage.Name, "TestExternalLogging")
65+
assertEqual(t, ml.LastMessage.Msg, "Basic logging message here")
66+
67+
log.Info("Basic logging message here with number %v", 1234)
68+
assertEqual(t, ml.LastMessage.Prefix, "INFO")
69+
assertEqual(t, ml.LastMessage.Name, "TestExternalLogging")
70+
assertEqual(t, ml.LastMessage.Msg, "Basic logging message here with number 1234")
71+
72+
log.Warn("With a string and a number: %s, %d", "a string", 5678)
73+
assertEqual(t, ml.LastMessage.Prefix, "WARN")
74+
assertEqual(t, ml.LastMessage.Name, "TestExternalLogging")
75+
assertEqual(t, ml.LastMessage.Msg, "With a string and a number: a string, 5678")
76+
77+
log.LineTrace()
78+
assertEqual(t, ml.LastMessage.Prefix, "TRACE")
79+
assertEqual(t, ml.LastMessage.Name, "TestExternalLogging")
80+
}

0 commit comments

Comments
 (0)