Blame view

main.go 2.99 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"
829723543   Marko Tikvić   Improved tracing....
14
  const thisFile = ""
b45f88e17   Marko Tikvić   more readable dat...
15

92485819d   Marko Tikvić   lint
16
  // Block ...
434e7da25   Marko Tikvić   changed initializ...
17
18
19
20
21
22
23
  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
24

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

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

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

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

98cf2ceeb   Marko Tikvić   new interaface; a...
132
  	// open new file
c138614d9   Marko Tikvić   output directory ...
133
134
135
  	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
136

c138614d9   Marko Tikvić   output directory ...
137
  	return err
a99c98307   Marko Tikvić   first commit
138
  }
98cf2ceeb   Marko Tikvić   new interaface; a...
139
140
141
  func (l *Logger) shouldSplit(nextEntrySize int) bool {
  	stats, _ := l.outputFile.Stat()
  	return int64(nextEntrySize) >= (l.maxFileSize - stats.Size())
a99c98307   Marko Tikvić   first commit
142
  }
a7081e581   Marko Tikvić   pretty print json
143
144
145
146
147
148
  
  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....
149
150
151
152
153
  
  // GetOutDir ...
  func (l *Logger) GetOutDir() string {
  	return l.directory
  }