mysqlhook

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2023 License: MIT Imports: 8 Imported by: 0

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExecExtraItem

type ExecExtraItem struct {
	Field  string // field name
	DBType string // mysql data type
}

ExecExtraItem extra item

func NewExecExtraItem

func NewExecExtraItem(field, dbType string) *ExecExtraItem

NewExecExtraItem create extra item instance

type Execer

type Execer interface {
	Exec(entry *logrus.Entry) error
}

Execer write the logrus entry to the database

func NewExec

func NewExec(db *sql.DB, tableName string, extraItems ...*ExecExtraItem) Execer

NewExec create an exec instance

type FilterHandle

type FilterHandle func(*logrus.Entry) *logrus.Entry

FilterHandle a filter handler

type Hook

type Hook struct {
	// contains filtered or unexported fields
}

Hook to send logs to a mysql database

Example
db, err := sql.Open("mysql", dsn)
if err != nil {
	fmt.Println(err)
	return
}
defer db.Close()

tableName := "e_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

func Default

func Default(db *sql.DB, tableName string, opts ...Option) *Hook

Default create a default mysql hook

func DefaultWithExtra

func DefaultWithExtra(db *sql.DB, tableName string, extraItems []*ExecExtraItem, opts ...Option) *Hook

DefaultWithExtra create a default mysql hook with extra items

func New

func New(opt ...Option) *Hook

New creates a hook to be added to an instance of logger

func (*Hook) Fire

func (h *Hook) Fire(entry *logrus.Entry) error

Fire is called when a log event is fired

func (*Hook) Flush

func (h *Hook) Flush()

Flush waits for the log queue to be empty

func (*Hook) Levels

func (h *Hook) Levels() []logrus.Level

Levels returns the available logging levels

type Option

type Option func(*options)

Option a hook parameter options

func SetExec

func SetExec(exec Execer) Option

SetExec set the Execer interface

func SetExtra

func SetExtra(extra map[string]interface{}) Option

SetExtra set extended parameters

func SetFilter

func SetFilter(filter FilterHandle) Option

SetFilter set the entry filter

func SetLevels

func SetLevels(levels ...logrus.Level) Option

SetLevels set the available log level

func SetMaxQueues

func SetMaxQueues(maxQueues int) Option

SetMaxQueues set the number of buffers

func SetMaxWorkers

func SetMaxWorkers(maxWorkers int) Option

SetMaxWorkers set the number of worker threads

func SetOut

func SetOut(out io.Writer) Option

SetOut set error output

Jump to

Keyboard shortcuts

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