Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3: WIP #23

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Docs: http://editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
43 changes: 27 additions & 16 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
name: Go
on:
push:
branches: [master]
branches: [ main ]
paths:
- '**.go'
- 'go.mod'
- '.golangci.yml'
- '.github/workflows/go.yml'
pull_request:
paths:
- '**.go'
- 'go.mod'
- '.golangci.yml'
- '.github/workflows/go.yml'
env:
GOPROXY: "https://proxy.golang.org"

Expand All @@ -11,35 +21,36 @@ jobs:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.20.x
- name: Checkout code
uses: actions/checkout@v3
- name: Run golangci-lint
uses: actions-contrib/golangci-lint@v1
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --timeout=30m

test:
name: Test
strategy:
matrix:
go-version: [1.14.x, 1.15.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
go-version: [ 1.19.x, 1.20.x ]
platform: [ ubuntu-latest, macos-latest, windows-latest ]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v1
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Run unit tests
uses: actions/checkout@v3
- name: Run tests with coverage
run: go test -v -race -coverprofile=coverage -covermode=atomic ./...
- name: Upload coverage report to Codecov
uses: codecov/codecov-action@v1.0.6
uses: codecov/codecov-action@v1.5.0
with:
file: ./coverage
flags: unittests
- name: Cache downloaded modules
uses: actions/cache@v1
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
27 changes: 19 additions & 8 deletions .github/workflows/lsif.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
name: LSIF
on: [push]
on:
push:
paths:
- '**.go'
- 'go.mod'
- '.github/workflows/lsif.yml'
env:
GOPROXY: "https://proxy.golang.org"

jobs:
build:
lsif-go:
if: github.repository == 'go-clog/clog'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- name: Generate LSIF data
uses: sourcegraph/lsif-go-action@master
- name: Upload LSIF data to sourcegraph.com
continue-on-error: true
uses: docker://sourcegraph/src-cli:latest
with:
verbose: 'true'
- name: Upload LSIF data
uses: sourcegraph/lsif-upload-action@master
args: lsif upload -github-token=${{ secrets.GITHUB_TOKEN }}
- name: Upload LSIF data to cs.unknwon.dev
continue-on-error: true
uses: docker://sourcegraph/src-cli:latest
with:
endpoint: https://sourcegraph.com
github_token: ${{ secrets.GITHUB_TOKEN }}
args: -endpoint=https://cs.unknwon.dev lsif upload -github-token=${{ secrets.GITHUB_TOKEN }}
19 changes: 19 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
linters-settings:
nakedret:
max-func-lines: 0 # Disallow any unnamed return statement

linters:
enable:
- unused
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- nakedret
- gofmt
- rowserrcheck
- unconvert
- goimports
- unparam
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Clog
# Clog

[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/go-clog/clog/Go?logo=github&style=for-the-badge)](https://github.com/go-clog/clog/actions?query=workflow%3AGo)
[![codecov](https://img.shields.io/codecov/c/github/go-clog/clog/master?logo=codecov&style=for-the-badge)](https://codecov.io/gh/go-clog/clog)
Expand All @@ -13,10 +13,10 @@ This package supports multiple loggers across different levels of logging. It us

## Installation

The minimum requirement of Go is **1.11**.
The minimum requirement of Go is **1.19**.

go get unknwon.dev/clog/v2

Please apply `-u` flag to update in the future.

## Getting Started
Expand Down Expand Up @@ -51,7 +51,7 @@ The code inside `init` function is equivalent to the following:

```go
func init() {
err := log.NewConsole(0,
err := log.NewConsole(0,
log.ConsoleConfig{
Level: log.LevelTrace,
},
Expand All @@ -66,7 +66,7 @@ Or expand further:

```go
func init() {
err := log.NewConsoleWithName(log.DefaultConsoleName, 0,
err := log.NewConsoleWithName(log.DefaultConsoleName, 0,
log.ConsoleConfig{
Level: log.LevelTrace,
},
Expand All @@ -85,7 +85,7 @@ In production, you may want to make log less verbose and be asynchronous:

```go
func init() {
// The buffer size mainly depends on number of logs could be produced at the same time,
// The buffer size mainly depends on number of logs could be produced at the same time,
// 100 is a good default.
err := log.NewConsole(100,
log.ConsoleConfig{
Expand Down Expand Up @@ -145,7 +145,7 @@ func main() {

### Caller Location

When using `log.Error` and `log.Fatal` functions, the caller location is written along with logs.
When using `log.Error` and `log.Fatal` functions, the caller location is written along with logs.

```go
func main() {
Expand All @@ -171,10 +171,10 @@ File logger is the single most powerful builtin logger, it has the ability to ro

```go
func init() {
err := log.NewFile(100,
err := log.NewFile(100,
log.FileConfig{
Level: log.LevelInfo,
Filename: "clog.log",
Filename: "clog.log",
FileRotationConfig: log.FileRotationConfig {
Rotate: true,
Daily: true,
Expand Down
18 changes: 9 additions & 9 deletions clog.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Level int

// Available logging levels.
const (
LevelTrace Level = iota
LevelDebug Level = iota
LevelInfo
LevelWarn
LevelError
Expand All @@ -20,8 +20,8 @@ const (

func (l Level) String() string {
switch l {
case LevelTrace:
return "TRACE"
case LevelDebug:
return "DEBUG"
case LevelInfo:
return "INFO"
case LevelWarn:
Expand All @@ -36,9 +36,9 @@ func (l Level) String() string {
}
}

// Trace writes formatted log in Trace level.
func Trace(format string, v ...interface{}) {
mgr.write(LevelTrace, 0, format, v...)
// Debug writes formatted log in Debug level.
func Debug(format string, v ...interface{}) {
mgr.write(LevelDebug, 0, format, v...)
}

// Info writes formatted log in Info level.
Expand Down Expand Up @@ -85,9 +85,9 @@ func FatalDepth(skip int, format string, v ...interface{}) {
exit()
}

// TraceTo writes formatted log in Trace level to the logger with given name.
func TraceTo(name, format string, v ...interface{}) {
mgr.writeTo(name, LevelTrace, 0, format, v...)
// DebugTo writes formatted log in Debug level to the logger with given name.
func DebugTo(name, format string, v ...interface{}) {
mgr.writeTo(name, LevelDebug, 0, format, v...)
}

// InfoTo writes formatted log in Info level to the logger with given name.
Expand Down
14 changes: 7 additions & 7 deletions clog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func chanLoggerIniter(name string, level Level) Initer {

func Test_chanLogger(t *testing.T) {
test1 := "mode1"
test1Initer := chanLoggerIniter(test1, LevelTrace)
test1Initer := chanLoggerIniter(test1, LevelDebug)

test2 := "mode2"
test2Initer := chanLoggerIniter(test2, LevelError)
Expand All @@ -87,8 +87,8 @@ func Test_chanLogger(t *testing.T) {
}{
{
name: "trace",
fn: Trace,
containsStr1: "[TRACE] log message",
fn: Debug,
containsStr1: "[DEBUG] log message",
containsStr2: "",
},
{
Expand Down Expand Up @@ -133,10 +133,10 @@ func Test_chanLogger(t *testing.T) {

func Test_writeToNamedLogger(t *testing.T) {
test1 := "alice"
test1Initer := chanLoggerIniter(test1, LevelTrace)
test1Initer := chanLoggerIniter(test1, LevelDebug)

test2 := "bob"
test2Initer := chanLoggerIniter(test2, LevelTrace)
test2Initer := chanLoggerIniter(test2, LevelDebug)

c1 := make(chan string)
c2 := make(chan string)
Expand All @@ -158,8 +158,8 @@ func Test_writeToNamedLogger(t *testing.T) {
}{
{
name: "trace",
fn: TraceTo,
containsStr1: "[TRACE] log message",
fn: DebugTo,
containsStr1: "[DEBUG] log message",
containsStr2: "",
},
{
Expand Down
2 changes: 1 addition & 1 deletion console.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// consoleColors is the color set for different levels.
var consoleColors = []func(a ...interface{}) string{
color.New(color.FgBlue).SprintFunc(), // Trace
color.New(color.FgBlue).SprintFunc(), // Debug
color.New(color.FgGreen).SprintFunc(), // Info
color.New(color.FgYellow).SprintFunc(), // Warn
color.New(color.FgRed).SprintFunc(), // Error
Expand Down
2 changes: 1 addition & 1 deletion console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ func Test_consoleLogger(t *testing.T) {
assert.Equal(t, DefaultConsoleName, mgr.loggers[0].Name())
assert.Equal(t, LevelInfo, mgr.loggers[0].Level())
assert.Equal(t, testName, mgr.loggers[1].Name())
assert.Equal(t, LevelTrace, mgr.loggers[1].Level())
assert.Equal(t, LevelDebug, mgr.loggers[1].Level())
}
11 changes: 5 additions & 6 deletions discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
Expand All @@ -28,15 +27,15 @@ type (

var (
discordTitles = []string{
"Trace",
"Debug",
"Information",
"Warning",
"Error",
"Fatal",
}

discordColors = []int{
0, // Trace
0, // Debug
3843043, // Info
16761600, // Warn
13041721, // Error
Expand All @@ -54,10 +53,10 @@ type DiscordConfig struct {
// Leave empty to use default as set in the Discord.
Username string
// Title for different levels, must have exact 5 elements in the order of
// Trace, Info, Warn, Error, and Fatal.
// Debug, Info, Warn, Error, and Fatal.
Titles []string
// Colors for different levels, must have exact 5 elements in the order of
// Trace, Info, Warn, Error, and Fatal.
// Debug, Info, Warn, Error, and Fatal.
Colors []int
}

Expand Down Expand Up @@ -117,7 +116,7 @@ func (l *discordLogger) postMessage(r io.Reader) (int64, error) {

return rateLimitMsg.RetryAfter, nil
} else if resp.StatusCode/100 != 2 {
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return -1, fmt.Errorf("read HTTP response body: %v", err)
}
Expand Down
Loading