noagorm

package module
v1.0.1 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: 0

README

Noa Gorm

The Gorm integration module for Noa Log, enabling quick integration of Noa into Gorm to print SQL logs and more in a unified manner.

Installation

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

Quick Start

package main

import (
    "github.com/noa-log/noa"
    noagorm "github.com/noa-log/noa-gorm"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
)

func main() {
    // Create a new Noa logger instance
    logger := noa.NewLog()

    db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
        Logger: noagorm.New(logger), // Replace the default logger with Noa Gorm logger
    })
    if err != nil {
        panic("failed to connect database")
    }

    // ... perform database operations
}

Configuration

Noa Gorm shares configuration with the Noa instance. Some sql log options can be customized within the Gorm logger instance.

package main

import (
    "time"
    "github.com/noa-log/noa"
    noagorm "github.com/noa-log/noa-gorm"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
)

func main() {
    // Create a new Noa logger instance
    logger := noa.NewLog()

    // Create a Gorm logger using the Noa instance
    gormLogger := noagorm.New(logger)

    // Ignore 'record not found' errors
    gormLogger.IgnoreRecordNotFoundError = true
    // Set the threshold for slow SQL logs
    gormLogger.SlowThreshold = 200 * time.Millisecond

    db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
        Logger: gormLogger, // Replace the default logger with Noa Gorm logger
    })
    if err != nil {
        panic("failed to connect database")
    }

    // ... perform database operations
}

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-09 16:20:09 * @LastEditTime: 2025-06-09 17:03:18 * @LastEditors: nijineko * @Description: noa gorm log * @FilePath: \noa-gorm\log.go

* @Author: nijineko * @Date: 2025-06-09 16:30:57 * @LastEditTime: 2025-06-11 12:01:11 * @LastEditors: nijineko * @Description: noa gorm log * @FilePath: \noa-gorm\noagorm.go

Index

Constants

View Source
const (
	TraceFormat     = "%s | %.3fms | rows:%v | %s"
	TraceWarnFormat = "%s | %.3fms | rows:%v | %s\n%s"
	TraceErrFormat  = "%s | %.3fms | rows:%v | %s\n"
)

Variables

View Source
var (
	DEFAULT_LOG_SOURCE = "Gorm"
)

Functions

func New

func New(Log *noa.LogConfig) *gormLogger

*

  • @description: Create a new gorm logger instance
  • @param {noa.LogConfig} Log noa log configuration
  • @return {*gormLogger} gorm logger instance

Types

This section is empty.

Jump to

Keyboard shortcuts

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