logger

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2025 License: MIT Imports: 8 Imported by: 0

README

logger

Simple structured logging library for Go applications. Supports INFO, WARN, ERROR levels, file output, timestamps, caller info, and PID.

Installation

go get github.com/73ddy-io/logger

Usage

package main

import (
    "github.com/73ddy-io/logger"
)

func main() {
    logger.InitLogger("app.log")
    defer logger.Close()
    
    logger.Info("Application started")
    logger.Warn("Warning: low memory")
    logger.Error("Critical error: %s", "database unavailable")
}

Log Format

2025-01-02 15:04:05 [INFO] 1234 main.go:12 main.main - Application started

Functions

  • InitLogger(filename string) error — initializes logger with file
  • Close() error — closes log file
  • Info(format string, args ...interface{})
  • Warn(format string, args ...interface{})
  • Error(format string, args ...interface{})

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

Close closes the underlying log file if it is open.

It should be called when the application is shutting down to ensure that all buffered log data is flushed and the file descriptor is released.

func Error

func Error(format string, args ...interface{})

Error logs an error message using printf-style formatting.

Use this for error conditions and failures that should be visible in application logs.

func Info

func Info(format string, args ...interface{})

Info logs an informational message using printf-style formatting.

The format string and arguments are passed to fmt.Sprintf and the resulting string is logged with INFO level.

func InitLogger

func InitLogger(filename string) error

InitLogger initializes the global logger with the given log file path.

The function ensures that the directory for the log file exists, creates it if necessary, and then opens the log file for appending. If the logger is successfully initialized, subsequent logging functions (Info, Warn, Error) will write to this file.

func Log

func Log(level LogLevel, message string)

Log writes a formatted log entry with the given level and message.

Log automatically captures information about the caller (file name, line number, and function name) and prepends a timestamp and process ID. It is the low-level logging function that is wrapped by Info, Warn, and Error.

func Warn

func Warn(format string, args ...interface{})

Warn logs a warning message using printf-style formatting.

This should be used for situations that are not fatal but may require attention or indicate a potential problem.

Types

type LogLevel

type LogLevel int

LogLevel represents the severity level of a log message.

const (
	INFO LogLevel = iota
	WARN
	ERROR
)

Available log levels.

INFO is used for general informational messages. WARN is used for non-critical issues that might require attention. ERROR is used for errors and failures.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL