Skip to content

Commit

Permalink
update storage logger
Browse files Browse the repository at this point in the history
  • Loading branch information
dieofcode committed Jan 14, 2024
1 parent 66b12bc commit 42d65da
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {

logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
config := configuration.ServerConfiguration()
memStorage := storage.NewMemStorage()
memStorage := storage.NewMemStorage(logger)
handler := handler.NewHandler(memStorage, logger)

router := chi.NewRouter()
Expand Down
10 changes: 7 additions & 3 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"sync"

"github.com/DieOfCode/go-alert-service/internal/metrics"
"github.com/rs/zerolog"
)

type MemStorage struct {
mu sync.Mutex
metrics map[string]metrics.Metrics
logger zerolog.Logger
}

type Repository interface {
Expand All @@ -19,9 +21,10 @@ type Repository interface {
GetAllMetrics() []metrics.Metrics
}

func NewMemStorage() *MemStorage {
func NewMemStorage(logger zerolog.Logger) *MemStorage {
return &MemStorage{
metrics: make(map[string]metrics.Metrics),
logger: logger,
}
}

Expand All @@ -30,12 +33,11 @@ func (storage *MemStorage) UpdateMetric(metricType string, metricName string, va
defer storage.mu.Unlock()

key := metricName
fmt.Print("KEY\n")
fmt.Print(key)
switch metricType {
case metrics.Gauge:
if newValue, err := strconv.ParseFloat(value, 64); err == nil {
storage.metrics[key] = metrics.Metrics{Value: &newValue}
storage.logger.Info().Msgf("STORAGE GAUDE UPDATE: %s %s %s %v", metricType, metricName, key, newValue)
} else {
return fmt.Errorf("некорректное значение для типа counter: %v", value)

Expand All @@ -54,6 +56,8 @@ func (storage *MemStorage) UpdateMetric(metricType string, metricName string, va
updatedValue := firstSum + newValue
storage.metrics[key] = metrics.Metrics{Delta: &updatedValue}

storage.logger.Info().Msgf("STORAGE COUNTER UPDATE: %s %s %s %v", metricType, metricName, key, updatedValue)

} else {
if newValue, err := strconv.ParseInt(value, 10, 64); err == nil {
storage.metrics[key] = metrics.Metrics{Delta: &newValue}
Expand Down

0 comments on commit 42d65da

Please sign in to comment.