Skip to content

Commit

Permalink
Update lagra.go
Browse files Browse the repository at this point in the history
  • Loading branch information
simplyYan authored Sep 10, 2023
1 parent d906de3 commit 125e9f7
Showing 1 changed file with 65 additions and 67 deletions.
132 changes: 65 additions & 67 deletions lagra.go
Original file line number Diff line number Diff line change
@@ -1,103 +1,101 @@
package lagra

import (
"context"
"fmt"
"os"
"sync"
"time"
"context"
"fmt"
"os"
"sync"
"time"

"github.com/fatih/color"
"github.com/fatih/color"
)

type LogType string

const (
Info LogType = "INFO"
Warn LogType = "WARN"
Error LogType = "ERROR"
Info LogType = "INFO"
Warn LogType = "WARN"
Error LogType = "ERROR"
)

type Lagra struct {
logFile *os.File
logMutex sync.Mutex
logFile *os.File
logMutex sync.Mutex
}

func New() *Lagra {
return &Lagra{}
return &Lagra{}
}

func (l *Lagra) send(logType LogType) func(ctx context.Context, message string) error {
return func(ctx context.Context, message string) error {
l.logMutex.Lock()
defer l.logMutex.Unlock()

logMessage := fmt.Sprintf("%s - %s - %s\n", time.Now().Format("15:04.05.9 - 02/01/2006"), logType, message)

var textColor *color.Color
switch logType {
case Info:
textColor = color.New(color.FgGreen)
case Warn:
textColor = color.New(color.FgYellow)
case Error:
textColor = color.New(color.FgRed)
default:
textColor = color.New(color.Reset)
}

logMessageColored := textColor.SprintFunc()(logMessage)
fmt.Print(logMessageColored) // Imprime com cores

if l.logFile != nil {
_, err := l.logFile.WriteString(logMessage)
if err != nil {
return err // Retornar erro se ocorrer um problema ao escrever no arquivo de log
}
}

return nil // Sem erro
}
func (l *Lagra) send(logType LogType, message string) error {
l.logMutex.Lock()
defer l.logMutex.Unlock()

logMessage := fmt.Sprintf("%s - %s - %s\n", time.Now().Format("15:04.05.9 - 02/01/2006"), logType, message)

var textColor *color.Color
switch logType {
case Info:
textColor = color.New(color.FgGreen)
case Warn:
textColor = color.New(color.FgYellow)
case Error:
textColor = color.New(color.FgRed)
default:
textColor = color.New(color.Reset)
}

logMessageColored := textColor.SprintFunc()(logMessage)
fmt.Print(logMessageColored) // Imprime com cores

if l.logFile != nil {
_, err := l.logFile.WriteString(logMessage)
if err != nil {
return err // Retornar erro se ocorrer um problema ao escrever no arquivo de log
}
}

return nil // Sem erro
}

// Info logs an informational message.
func (l *Lagra) Info(ctx context.Context, message string) error {
return l.send(Info)(ctx, message)
return l.send(Info, message)
}

// Warn logs a warning message.
func (l *Lagra) Warn(ctx context.Context, message string) error {
return l.send(Warn)(ctx, message)
return l.send(Warn, message)
}

// Error logs an error message.
func (l *Lagra) Error(ctx context.Context, message string) error {
return l.send(Error)(ctx, message)
return l.send(Error, message)
}

func (l *Lagra) save() error {
l.logMutex.Lock()
defer l.logMutex.Unlock()

if l.logFile == nil {
var err error
l.logFile, err = os.Create("log.lagra")
if err != nil {
return err // Retornar erro se ocorrer um problema ao criar o arquivo de log
}
}
return nil // Sem erro
l.logMutex.Lock()
defer l.logMutex.Unlock()

if l.logFile == nil {
var err error
l.logFile, err = os.Create("log.lagra")
if err != nil {
return err // Retornar erro se ocorrer um problema ao criar o arquivo de log
}
}
return nil // Sem erro
}

func (l *Lagra) Close() error {
l.logMutex.Lock()
defer l.logMutex.Unlock()

if l.logFile != nil {
err := l.logFile.Close()
if err != nil {
return err // Retornar erro se ocorrer um problema ao fechar o arquivo de log
}
}
return nil // Sem erro
l.logMutex.Lock()
defer l.logMutex.Unlock()

if l.logFile != nil {
err := l.logFile.Close()
if err != nil {
return err // Retornar erro se ocorrer um problema ao fechar o arquivo de log
}
}
return nil // Sem erro
}

0 comments on commit 125e9f7

Please sign in to comment.