@@ -9,86 +9,110 @@ import (
99 "github.com/fatih/color"
1010)
1111
12- // Logger provides timestamped logging functionality
13- type Logger struct {
14- startTime time.Time
15- stdout io.Writer
16- stderr io.Writer
12+ // Logger provides logging functionality.
13+ type Logger interface {
14+ Info (message string )
15+ Infof (format string , args ... interface {})
16+ Error (message string )
17+ Errorf (format string , args ... interface {})
18+ Success (message string )
19+ Successf (format string , args ... interface {})
20+ Warning (message string )
21+ Warningf (format string , args ... interface {})
22+ Dim (message string )
23+ Dimf (format string , args ... interface {})
1724}
1825
19- // New creates a new Logger instance
20- func New () * Logger {
21- return & Logger {
26+ type LoggerImpl struct {
27+ startTime time.Time
28+ stdout io.Writer
29+ stderr io.Writer
30+ withTimestamps bool
31+ }
32+
33+ // New creates a new Logger instance.
34+ func New () * LoggerImpl {
35+ return & LoggerImpl {
2236 startTime : time .Now (),
2337 stdout : os .Stdout ,
2438 stderr : os .Stderr ,
2539 }
2640}
2741
28- // getTimestamp returns the elapsed time since logger creation in MM:SS format
29- func (l * Logger ) getTimestamp () string {
42+ // NewWithTimestamps creates a new Logger instance with timestamps.
43+ func NewWithTimestamps () * LoggerImpl {
44+ logger := New ()
45+ logger .withTimestamps = true
46+ return logger
47+ }
48+
49+ // getTimestamp returns the elapsed time since logger creation in MM:SS format.
50+ func (l * LoggerImpl ) getTimestamp () string {
51+ if ! l .withTimestamps {
52+ return ""
53+ }
3054 elapsed := time .Since (l .startTime )
3155 minutes := int (elapsed .Minutes ())
3256 seconds := int (elapsed .Seconds ()) % 60
33- return fmt .Sprintf ("%02d:%02d" , minutes , seconds )
57+ return fmt .Sprintf ("%02d:%02d " , minutes , seconds )
3458}
3559
3660// Info prints an info message with magenta styling
37- func (l * Logger ) Info (message string ) {
61+ func (l * LoggerImpl ) Info (message string ) {
3862 timestamp := color .GreenString (l .getTimestamp ())
3963 info := color .New (color .FgMagenta , color .Bold ).Sprint (message )
40- fmt .Fprintf (l .stdout , "%s %s\n " , timestamp , info )
64+ fmt .Fprintf (l .stdout , "%s%s\n " , timestamp , info )
4165}
4266
4367// Infof prints a formatted info message with magenta styling
44- func (l * Logger ) Infof (format string , args ... interface {}) {
68+ func (l * LoggerImpl ) Infof (format string , args ... interface {}) {
4569 l .Info (fmt .Sprintf (format , args ... ))
4670}
4771
4872// Error prints an error message with red styling to stderr
49- func (l * Logger ) Error (message string ) {
73+ func (l * LoggerImpl ) Error (message string ) {
5074 timestamp := color .GreenString (l .getTimestamp ())
5175 errMsg := color .New (color .FgRed , color .Bold ).Sprint (message )
52- fmt .Fprintf (l .stderr , "%s %s\n " , timestamp , errMsg )
76+ fmt .Fprintf (l .stderr , "%s%s\n " , timestamp , errMsg )
5377}
5478
5579// Errorf prints a formatted error message with red styling to stderr
56- func (l * Logger ) Errorf (format string , args ... interface {}) {
80+ func (l * LoggerImpl ) Errorf (format string , args ... interface {}) {
5781 l .Error (fmt .Sprintf (format , args ... ))
5882}
5983
6084// Success prints a success message with green styling
61- func (l * Logger ) Success (message string ) {
85+ func (l * LoggerImpl ) Success (message string ) {
6286 timestamp := color .GreenString (l .getTimestamp ())
6387 success := color .New (color .FgGreen , color .Bold ).Sprint (message )
64- fmt .Fprintf (l .stdout , "%s %s\n " , timestamp , success )
88+ fmt .Fprintf (l .stdout , "%s%s\n " , timestamp , success )
6589}
6690
6791// Successf prints a formatted success message with green styling
68- func (l * Logger ) Successf (format string , args ... interface {}) {
92+ func (l * LoggerImpl ) Successf (format string , args ... interface {}) {
6993 l .Success (fmt .Sprintf (format , args ... ))
7094}
7195
7296// Warning prints a warning message with yellow styling
73- func (l * Logger ) Warning (message string ) {
97+ func (l * LoggerImpl ) Warning (message string ) {
7498 timestamp := color .GreenString (l .getTimestamp ())
7599 warning := color .New (color .FgYellow , color .Bold ).Sprint (message )
76- fmt .Fprintf (l .stdout , "%s %s\n " , timestamp , warning )
100+ fmt .Fprintf (l .stdout , "%s%s\n " , timestamp , warning )
77101}
78102
79103// Warning prints a formatted warning message with yellow styling
80- func (l * Logger ) Warningf (format string , args ... interface {}) {
104+ func (l * LoggerImpl ) Warningf (format string , args ... interface {}) {
81105 l .Warning (fmt .Sprintf (format , args ... ))
82106}
83107
84108// Dim prints a dimmed message
85- func (l * Logger ) Dim (message string ) {
109+ func (l * LoggerImpl ) Dim (message string ) {
86110 timestamp := color .GreenString (l .getTimestamp ())
87111 dim := color .New (color .Faint ).Sprint (message )
88- fmt .Fprintf (l .stdout , "%s %s\n " , timestamp , dim )
112+ fmt .Fprintf (l .stdout , "%s%s\n " , timestamp , dim )
89113}
90114
91115// Dimf prints a formatted dimmed message
92- func (l * Logger ) Dimf (format string , args ... interface {}) {
116+ func (l * LoggerImpl ) Dimf (format string , args ... interface {}) {
93117 l .Dim (fmt .Sprintf (format , args ... ))
94118}
0 commit comments