noa

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2025 License: Apache-2.0 Imports: 8 Imported by: 6

README

Noa

A Golang logging library that supports basic log printing, recording, automatic log rotation, and more.
It can be quickly integrated into existing projects and offers flexible configuration options and extensibility.

Installation

go get -u github.com/noa-log/noa

Quick Start

package main

import (
    "errors"
    "github.com/noa-log/noa"
)

func main() {
    // Create a new logger instance
    logger := noa.NewLog()
    // Configure the logger, such as setting the log level, format, etc.
    logger.Level = noa.INFO

    // Print logs
    logger.Debug("Test", "This is a Debug log")
    logger.Info("Test", "This is an Info log")
    logger.Warning("Test", "This is a Warn log")
    logger.Error("Test", "This is an Error log")
    logger.Fatal("Test", "This is a Fatal log")

    // Print an error
    err := errors.New("An example error")
    logger.Error("Test", err)
}

For more usage details and configuration options, please refer to the Documentation

Features

  • Supports multiple log levels
  • Supports automatic log rotation based on time
  • Supports log formatting
  • Supports handle before and after printing logs, allowing for modification or reporting of log information
  • Provides extensive configuration options to customize logging behavior
  • Supports integration with some third-party libraries such as Gin, Gorm, etc.
  • Enhances error context information by wrapping errors

Integrations

Here are some officially maintained integration libraries that provide Noa support for popular frameworks and libraries:

  • noa-gin - Integrate Noa with the Gin framework
  • noa-echo - Integrate Noa with the Echo framework
  • noa-gorm - Integrate Noa with Gorm
  • noa-sentry - Integrate Noa with Sentry

License

This project is open-sourced under the Apache License 2.0. Please comply with the terms when using it.

Documentation

Overview

* @Author: nijineko * @Date: 2025-06-08 11:03:48 * @LastEditTime: 2025-06-08 17:05:59 * @LastEditors: nijineko * @Description: log file handle utility package * @FilePath: \noa\file.go

* @Author: nijineko * @Date: 2025-06-08 10:52:00 * @LastEditTime: 2025-06-08 13:14:48 * @LastEditors: nijineko * @Description: log handle package * @FilePath: \noa\handle.go

* @Author: nijineko * @Date: 2025-06-08 10:29:01 * @LastEditTime: 2025-06-15 10:56:23 * @LastEditors: nijineko * @Description: noa log package * @FilePath: \noa\log.go

* @Author: nijineko * @Date: 2025-06-08 10:59:01 * @LastEditTime: 2025-06-15 10:49:36 * @LastEditors: nijineko * @Description: log output package * @FilePath: \noa\output.go

* @Author: nijineko * @Date: 2025-06-10 21:24:26 * @LastEditTime: 2025-06-10 22:29:29 * @LastEditors: nijineko * @Description: text log encoder * @FilePath: \noa\textEncoder.go

Index

Constants

View Source
const (
	DEBUG = iota + 0
	INFO
	WARNING
	ERROR
	FATAL
	OFF
)

default log levels

Variables

This section is empty.

Functions

This section is empty.

Types

type AfterHandleFunc

type AfterHandleFunc func(Level int, Source string, Data ...any) error

type BeforeHandleFunc

type BeforeHandleFunc func(Level *int, Source *string, Data ...*any) error

type LogConfig

type LogConfig struct {
	Level       int              // log level
	RemoveColor bool             // remove color from log output
	TimeFormat  string           // log prefix time format
	Errors      LogConfigErrors  // error configuration for logging
	Writer      LogConfigWriter  // writer configuration for logging to files
	Encoder     LogConfigEncoder // log encoder
	// contains filtered or unexported fields
}

Log config structure

func NewLog

func NewLog() *LogConfig

*

  • @description: Create a new log configuration instance
  • @return {*LogConfig} a log configuration instance

func (*LogConfig) AddAfterHandle

func (l *LogConfig) AddAfterHandle(HandleFunc AfterHandleFunc)

*

  • @description: Add a handle function to run after logging
  • @description: if error returned, will panic
  • @param {AfterHandleFunc} HandleFunc

func (*LogConfig) AddBeforeHandle

func (l *LogConfig) AddBeforeHandle(HandleFunc BeforeHandleFunc)

*

  • @description: Add a handle function to run before logging
  • @description: if error returned, will panic
  • @param {BeforeHandleFunc} HandleFun

func (*LogConfig) Debug

func (l *LogConfig) Debug(Source string, Data ...any)

*

  • @description: print debug log
  • @param {string} Source Log source (e.g., file name, function name)
  • @param {...any} Data print data

func (*LogConfig) Error

func (l *LogConfig) Error(Source string, Data ...any)

*

  • @description: print error log
  • @param {string} Source Log source (e.g., file name, function name)
  • @param {...any} Data print data

func (*LogConfig) Fatal

func (l *LogConfig) Fatal(Source string, Data ...any)

*

  • @description: print fatal log
  • @param {string} Source Log source (e.g., file name, function name)
  • @param {...any} Data print data

func (*LogConfig) Info

func (l *LogConfig) Info(Source string, Data ...any)

*

  • @description: print info log
  • @param {string} Source Log source (e.g., file name, function name)
  • @param {...any} Data print data

func (*LogConfig) Print

func (l *LogConfig) Print(Level int, Source string, Data ...any)

*

  • @description: Print log
  • @param {int} Level Log level
  • @param {string} Source Log source (e.g., file name, function name)
  • @param {...any} Data print data

func (*LogConfig) SetEncoder

func (l *LogConfig) SetEncoder(Encoder encoder.Encoder)

*

  • @description: Set encoder for the log instance
  • @param {encoder.Encoder} Encoder encoder instance

func (*LogConfig) Warning

func (l *LogConfig) Warning(Source string, Data ...any)

*

  • @description: print warning log
  • @param {string} Source Log source (e.g., file name, function name)
  • @param {...any} Data print data

type LogConfigEncoder

type LogConfigEncoder struct {
	Print encoder.Encoder
	Write encoder.Encoder
}

Log config encoder structure

type LogConfigErrors

type LogConfigErrors struct {
	StackTrace bool // whether to print stack trace for errors
	CallerSkip int  // skip number of stack frames to find the caller
}

Log config error structure

type LogConfigWriter

type LogConfigWriter struct {
	Enable     bool   // enable log file writing
	FolderPath string // folder path for log files
	TimeFormat string // time format for log file names
	// contains filtered or unexported fields
}

Log config writer structure

type TextEncoder

type TextEncoder struct {
	Newline TextEncoderConfigNewline // newline configuration

	Log *LogConfig // noa log instance
}

TextEncoder struct

func NewTextEncoder

func NewTextEncoder(Log *LogConfig) *TextEncoder

*

  • @description: Create a new TextEncoder instance
  • @return {*TextEncoder} TextEncoder instance

func (*TextEncoder) Print

func (te *TextEncoder) Print(c *encoder.Context)

print log data

func (*TextEncoder) Write

func (te *TextEncoder) Write(FileHandle *os.File, c *encoder.Context) error

write log data to file

func (*TextEncoder) WriteFileExtension

func (te *TextEncoder) WriteFileExtension() string

return file extension for the encoded file

type TextEncoderConfigNewline

type TextEncoderConfigNewline struct {
	Auto  bool // enable automatic newline at the end of each log entry
	Smart bool // enable smart newline handling, auto checks if the last element ends with a newline and skips appending if present
}

Directories

Path Synopsis
* @Author: nijineko * @Date: 2025-06-10 21:56:37 * @LastEditTime: 2025-06-10 21:56:43 * @LastEditors: nijineko * @Description: noa encoder context package * @FilePath: \noa\encoder\context.go
* @Author: nijineko * @Date: 2025-06-10 21:56:37 * @LastEditTime: 2025-06-10 21:56:43 * @LastEditors: nijineko * @Description: noa encoder context package * @FilePath: \noa\encoder\context.go
* @Author: nijineko * @Date: 2025-06-08 12:42:57 * @LastEditTime: 2025-06-12 11:10:17 * @LastEditors: nijineko * @Description: noa errors package * @FilePath: \noa\errors\errors.go
* @Author: nijineko * @Date: 2025-06-08 12:42:57 * @LastEditTime: 2025-06-12 11:10:17 * @LastEditors: nijineko * @Description: noa errors package * @FilePath: \noa\errors\errors.go
* @Author: nijineko * @Date: 2025-06-10 12:12:04 * @LastEditTime: 2025-06-10 12:12:05 * @LastEditors: nijineko * @Description: pad space util * @FilePath: \noa\tools\padSpace.go
* @Author: nijineko * @Date: 2025-06-10 12:12:04 * @LastEditTime: 2025-06-10 12:12:05 * @LastEditors: nijineko * @Description: pad space util * @FilePath: \noa\tools\padSpace.go

Jump to

Keyboard shortcuts

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