refactor: flatten repository structure to root and archive research notes
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package opslog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
dir string
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func New(dir string) *Logger {
|
||||
if dir == "" {
|
||||
return nil
|
||||
}
|
||||
return &Logger{dir: dir}
|
||||
}
|
||||
|
||||
func (l *Logger) Write(ip, user, action string) error {
|
||||
if l == nil || l.dir == "" {
|
||||
return nil
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if err := os.MkdirAll(l.dir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := os.OpenFile(filepath.Join(l.dir, "ops.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = fmt.Fprintf(f, "%s - %s - user(%s) %s\n", time.Now().Format("2006-01-02 15:04:05"), ip, user, action)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user