Blame view

main.go 2.98 KB
a99c98307   Marko Tikvić   first commit
1
2
3
  package gologger
  
  import (
a7081e581   Marko Tikvić   pretty print json
4
5
  	"bytes"
  	"encoding/json"
a99c98307   Marko Tikvić   first commit
6
7
  	"fmt"
  	"os"
e025a8738   Marko Tikvić   platform agnostic...
8
  	"path/filepath"
aa60a45ee   Marko Tikvić   changed log file ...
9
  	"strings"
3ba7b5695   Marko Tikvić   added mutex locks...
10
  	"sync"
baa4468b7   Marko Tikvić   Comments go befor...
11
  	"time"
a99c98307   Marko Tikvić   first commit
12
  )
b45f88e17   Marko Tikvić   more readable dat...
13
  const dateTimeFormat = "2006-01-02 15:04:05"
92485819d   Marko Tikvić   lint
14
  // Block ...
434e7da25   Marko Tikvić   changed initializ...
15
16
17
18
19
20
21
  const (
  	MaxLogSize5MB   int64 = 5 * 1024 * 1024
  	MaxLogSize1MB   int64 = 1 * 1024 * 1024
  	MaxLogSize500KB int64 = 500 * 1024
  	MaxLogSize100KB int64 = 100 * 1024
  	MaxLogSize512B  int64 = 512
  )
a99c98307   Marko Tikvić   first commit
22

92485819d   Marko Tikvić   lint
23
  // Logger ...
5eaa49d2a   Marko Tikvić   new design; allow...
24
  type Logger struct {
98cf2ceeb   Marko Tikvić   new interaface; a...
25
26
  	mu         *sync.Mutex
  	outputFile *os.File
23ce66f0f   Marko Tikvić   optional init par...
27

98cf2ceeb   Marko Tikvić   new interaface; a...
28
  	outputFileName string
3e936a3c2   Marko Tikvić   output file name ...
29
  	fullName       string
98cf2ceeb   Marko Tikvić   new interaface; a...
30
  	maxFileSize    int64
df74f4a7e   Marko Tikvić   new filename format
31
32
  
  	splitCount int
c138614d9   Marko Tikvić   output directory ...
33
34
  
  	directory string
a99c98307   Marko Tikvić   first commit
35
  }
92485819d   Marko Tikvić   lint
36
  // New ...
c138614d9   Marko Tikvić   output directory ...
37
  func New(name, dir string, maxFileSize int64) (logger *Logger, err error) {
5eaa49d2a   Marko Tikvić   new design; allow...
38
  	logger = &Logger{}
23ce66f0f   Marko Tikvić   optional init par...
39

b45f88e17   Marko Tikvić   more readable dat...
40
  	logger.outputFileName = name
98cf2ceeb   Marko Tikvić   new interaface; a...
41
42
  	logger.mu = &sync.Mutex{}
  	logger.maxFileSize = maxFileSize
c138614d9   Marko Tikvić   output directory ...
43
  	logger.directory = dir
a99c98307   Marko Tikvić   first commit
44

c138614d9   Marko Tikvić   output directory ...
45
  	err = os.Mkdir(dir, os.ModePerm)
98cf2ceeb   Marko Tikvić   new interaface; a...
46
47
  	if err != nil {
  		if !os.IsExist(err) {
5eaa49d2a   Marko Tikvić   new design; allow...
48
  			return nil, err
a99c98307   Marko Tikvić   first commit
49
50
  		}
  	}
b45f88e17   Marko Tikvić   more readable dat...
51
52
  	date := strings.Replace(time.Now().Format(dateTimeFormat), ":", ".", -1)
  	logger.outputFileName += " " + date
3e936a3c2   Marko Tikvić   output file name ...
53
  	logger.fullName = logger.outputFileName + ".txt"
c138614d9   Marko Tikvić   output directory ...
54
55
  	path := filepath.Join(dir, logger.fullName)
  	if logger.outputFile, err = os.Create(path); err != nil {
98cf2ceeb   Marko Tikvić   new interaface; a...
56
  		return nil, err
e025a8738   Marko Tikvić   platform agnostic...
57
  	}
5eaa49d2a   Marko Tikvić   new design; allow...
58
  	return logger, nil
98708cdaf   Marko Tikvić   new version, does...
59
  }
92485819d   Marko Tikvić   lint
60
  // Log ...
df74f4a7e   Marko Tikvić   new filename format
61
  func (l *Logger) Log(format string, v ...interface{}) {
372beaf62   Marko Tikvić   refactored if sta...
62
63
64
65
66
67
68
69
  	if l.outputFile == nil {
  		return
  	}
  
  	l.mu.Lock()
  	defer l.mu.Unlock()
  
  	msg := fmt.Sprintf(format, v...)
b45f88e17   Marko Tikvić   more readable dat...
70
71
  	s := time.Now().Format(dateTimeFormat) + ": " + msg + "
  "
372beaf62   Marko Tikvić   refactored if sta...
72
73
  	if l.shouldSplit(len(s)) {
  		l.split()
23ce66f0f   Marko Tikvić   optional init par...
74
  	}
372beaf62   Marko Tikvić   refactored if sta...
75
  	l.outputFile.WriteString(s)
98708cdaf   Marko Tikvić   new version, does...
76
  }
92485819d   Marko Tikvić   lint
77
78
79
  // Print ...
  func (l *Logger) Print(format string, v ...interface{}) {
  	msg := fmt.Sprintf(format, v...)
b45f88e17   Marko Tikvić   more readable dat...
80
81
  	fmt.Printf("%s: %s
  ", time.Now().Format(dateTimeFormat), msg)
92485819d   Marko Tikvić   lint
82
  }
92485819d   Marko Tikvić   lint
83
  // Trace ...
829723543   Marko Tikvić   Improved tracing....
84
  func (l *Logger) Trace(format string, v ...interface{}) string {
92485819d   Marko Tikvić   lint
85
  	if l.outputFile == nil {
829723543   Marko Tikvić   Improved tracing....
86
  		return ""
92485819d   Marko Tikvić   lint
87
88
89
90
  	}
  
  	l.mu.Lock()
  	defer l.mu.Unlock()
901a92955   Marko Tikvić   added new line af...
91
92
  	s := getTrace(format, v...) + "
  "
92485819d   Marko Tikvić   lint
93
94
95
96
97
  
  	if l.shouldSplit(len(s)) {
  		l.split()
  	}
  	l.outputFile.WriteString(s)
829723543   Marko Tikvić   Improved tracing....
98
99
  
  	return s
92485819d   Marko Tikvić   lint
100
  }
901a92955   Marko Tikvić   added new line af...
101
102
103
104
105
106
  // PrintTrace ...
  func (l *Logger) PrintTrace(format string, v ...interface{}) {
  	s := getTrace(format, v...)
  	fmt.Printf("%s
  ", s)
  }
92485819d   Marko Tikvić   lint
107
108
  // PrintAndTrace ...
  func (l *Logger) PrintAndTrace(format string, v ...interface{}) {
829723543   Marko Tikvić   Improved tracing....
109
110
  	t := l.Trace(format, v...)
  	fmt.Println(t)
98708cdaf   Marko Tikvić   new version, does...
111
  }
92485819d   Marko Tikvić   lint
112
  // Close ...
c138614d9   Marko Tikvić   output directory ...
113
  func (l *Logger) Close() error {
372beaf62   Marko Tikvić   refactored if sta...
114
  	if l.outputFile == nil {
c138614d9   Marko Tikvić   output directory ...
115
  		return nil
372beaf62   Marko Tikvić   refactored if sta...
116
  	}
c138614d9   Marko Tikvić   output directory ...
117
  	return l.outputFile.Close()
a99c98307   Marko Tikvić   first commit
118
  }
c138614d9   Marko Tikvić   output directory ...
119
  func (l *Logger) split() error {
372beaf62   Marko Tikvić   refactored if sta...
120
  	if l.outputFile == nil {
c138614d9   Marko Tikvić   output directory ...
121
  		return nil
372beaf62   Marko Tikvić   refactored if sta...
122
  	}
98cf2ceeb   Marko Tikvić   new interaface; a...
123
124
125
  	// close old file
  	err := l.outputFile.Close()
  	if err != nil {
c138614d9   Marko Tikvić   output directory ...
126
  		return err
98cf2ceeb   Marko Tikvić   new interaface; a...
127
  	}
df74f4a7e   Marko Tikvić   new filename format
128

98cf2ceeb   Marko Tikvić   new interaface; a...
129
  	// open new file
c138614d9   Marko Tikvić   output directory ...
130
131
132
  	l.splitCount++
  	path := filepath.Join(l.directory, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)+".txt")
  	l.outputFile, err = os.Create(path)
a99c98307   Marko Tikvić   first commit
133

c138614d9   Marko Tikvić   output directory ...
134
  	return err
a99c98307   Marko Tikvić   first commit
135
  }
98cf2ceeb   Marko Tikvić   new interaface; a...
136
137
138
  func (l *Logger) shouldSplit(nextEntrySize int) bool {
  	stats, _ := l.outputFile.Stat()
  	return int64(nextEntrySize) >= (l.maxFileSize - stats.Size())
a99c98307   Marko Tikvić   first commit
139
  }
a7081e581   Marko Tikvić   pretty print json
140
141
142
143
144
145
  
  func printJSON(in []byte) (out []byte, err error) {
  	var buf bytes.Buffer
  	err = json.Indent(&buf, in, "", "    ")
  	return buf.Bytes(), err
  }
829723543   Marko Tikvić   Improved tracing....
146
147
148
149
150
  
  // GetOutDir ...
  func (l *Logger) GetOutDir() string {
  	return l.directory
  }