Categorygithub.com/nazip/logrus-mysql-hook
modulepackage
0.0.0-20230906121559-824d74f6c5fc
Repository: https://github.com/nazip/logrus-mysql-hook.git
Documentation: pkg.go.dev

# README

An asynchronous MySQL Hook for Logrus

Build Codecov ReportCard GoDoc License

Quick Start

Download and install

$ go get -u -v github.com/LyricTian/logrus-mysql-hook

Usage

import "github.com/LyricTian/logrus-mysql-hook"

// ...

mysqlHook := mysqlhook.Default(db,"log")

defer mysqlHook.Flush()

log := logrus.New()
log.AddHook(mysqlHook)

Examples

package main

import (
	"database/sql"
	"fmt"

	"github.com/LyricTian/logrus-mysql-hook"
	"github.com/sirupsen/logrus"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer db.Close()

	tableName := "t_log"
	mysqlHook := mysqlhook.Default(db, tableName)
	defer db.Exec(fmt.Sprintf("drop table %s", tableName))

	log := logrus.New()
	log.AddHook(mysqlHook)
	log.WithField("foo", "bar").Info("foo test")

	mysqlHook.Flush()

	var message string
	row := db.QueryRow(fmt.Sprintf("select message from %s", tableName))
	err = row.Scan(&message)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(message)

	// Output: foo test
}

Use Extra Item Examples

package main

import (
	"database/sql"
	"fmt"

	"github.com/LyricTian/logrus-mysql-hook"
	"github.com/sirupsen/logrus"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer db.Close()

	tableName := "t_log"
	extraItems := []*mysqlhook.ExecExtraItem{
		mysqlhook.NewExecExtraItem("type", "varchar(50)"),
		mysqlhook.NewExecExtraItem("user_id", "varchar(50)"),
	}
	mysqlHook := mysqlhook.DefaultWithExtra(db, tableName, extraItems)

	defer db.Exec(fmt.Sprintf("drop table %s", tableName))

	log := logrus.New()
	log.AddHook(mysqlHook)
	log.WithField("foo", "bar").
		WithField("type", "system").
		WithField("user_id", "admin").
		Info("foo test")

	mysqlHook.Flush()

	var (
		message string
		typ     string
		userID  string
	)
	row := db.QueryRow(fmt.Sprintf("select message,type,user_id from %s", tableName))
	err = row.Scan(&message, &typ, &userID)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("[%s-%s]:%s\n", typ, userID, message)

	// Output: [system-admin]:foo test
}

MIT License

Copyright (c) 2018 Lyric

# Functions

Default create a default mysql hook.
DefaultWithExtra create a default mysql hook with extra items.
New creates a hook to be added to an instance of logger.
NewExec create an exec instance.
NewExecExtraItem create extra item instance.
SetExec set the Execer interface.
SetExtra set extended parameters.
SetFilter set the entry filter.
SetLevels set the available log level.
SetMaxQueues set the number of buffers.
SetMaxWorkers set the number of worker threads.
SetOut set error output.

# Structs

ExecExtraItem extra item.
Hook to send logs to a mysql database.

# Interfaces

Execer write the logrus entry to the database.

# Type aliases

FilterHandle a filter handler.
Option a hook parameter options.