dl

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2022 License: MIT Imports: 21 Imported by: 0

README

dl - The logger not committed to Git for debug

Go Reference .github/workflows/ci.yml Go Report Card codecov MIT License

delog.gif

Description

Who doesn't write wrong codes? No one.
Then, programs don't work well, and developers write logs for debug to understand what happens.

However, some developers forget to delete their logs after resolving the problem and push their codes. In the worse case, the logs might be released.

dl is developed to resolve their problems.

Features

Installation

Go 1.17 or earlier

It doesn't contain a generics feature.

$ go install github.com/task4233/dl/cmd/dl@v1
Go 1.18
$ go install github.com/task4233/dl/v2/cmd/dl@main

Usage

  1. debug your codes with dl package

Playground

package main

import (
	"os"

	"github.com/task4233/dl/v2"
)

type U[T any] []T

func (t U[T]) append(v T) {
	t = append(t, v)
	// debug
	dl.Info(t)
}

func (t U[T]) change(v T) {
	t[0] = v
	// debug
	dl.FInfo(os.Stdout, t)
}

func main() {
	t := U[int]([]int{1, 3})
	t.append(5)
	t.change(5)
}


// Output:
// [DeLog] info: main.U[int]{1, 3, 5} (main.U[int]) prog.go:13
// [DeLog] info: main.U[int]{5, 3} (main.U[int]) prog.go:18
  1. Install dl
$ dl init .
  1. Just commit
  • delog is used in the file.
$ cat main.go 
package main

import (
	"os"

	"github.com/task4233/dl/v2"
)

type U[T any] []T

func (t U[T]) append(v T) {
	t = append(t, v)
	// debug
	dl.Info(t)
}

func (t U[T]) change(v T) {
	t[0] = v
	// debug
	dl.FInfo(os.Stdout, t)
}

func main() {
	t := U[int]([]int{1, 3})
	t.append(5)
	t.change(5)
}
  • invoke $ git commit
$ git add main.go
$ git commit -m "feat: add main.go"
remove dl from main.go # automatically removed
[master 975ecf9] feat: add main.go
 1 file changed, 12 insertions(+), 21 deletions(-)
 rewrite main.go (91%)
  • delog is removed automatically
$ git diff HEAD^
diff --git a/main.go b/main.go
index 90a78bd..0e28e8a 100644
--- a/main.go
+++ b/main.go
@@ -0,0 +1,27 @@
+package main
+
+import (
+       "os"
+
+       "github.com/task4233/dl/v2"
+)
+
+type U[T any] []T
+
+func (t U[T]) append(v T) {
+       t = append(t, v)
+       // debug
+
+}
+
+func (t U[T]) change(v T) {
+       t[0] = v
+       // debug
+
+}
+
+func main() {
+       t := U[int]([]int{1, 3})
+       t.append(5)
+       t.change(5)
+}
  • removed delog codes are restored(not commited)
$ cat main.go 
package main

import (
	"os"

	"github.com/task4233/dl/v2"
)

type U[T any] []T

func (t U[T]) append(v T) {
	t = append(t, v)
	// debug
	dl.Info(t)
}

func (t U[T]) change(v T) {
	t[0] = v
	// debug
	dl.FInfo(os.Stdout, t)
}

func main() {
	t := U[int]([]int{1, 3})
	t.append(5)
	t.change(5)
}
Remove dl from GitHooks
$ dl remove .

Contribution

Please feel free to make issues and pull requests.

Author

task4233

Documentation

Overview

Example
num := 123
name := "dl"
type MyInt int
var myNum MyInt = 123

dl.Fprintln(os.Stdout, "num: ", num)
dl.Println("num: ", num)
dl.Fprintf(os.Stdout, "name: %s\n", name)
dl.Printf("name: %s", name)
dl.FInfo(os.Stdout, myNum)
dl.Info(myNum)

zapLog, err := zap.NewDevelopment()
if err != nil {
	panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
var log logr.Logger = zapr.NewLogger(zapLog)
dlr := dl.NewLogger(&log)
dlr.Info("Info: ", "num", num)
Output:

num:  123
name: dl
[DeLog] info: 123 (dl_test.MyInt) log_example_test.go:23

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FInfo

func FInfo[T any](w io.Writer, v T) (int, error)

FInfo gives a val, a type, a file name, a line number and writes to w..

Example
type person struct {
	name string
	age  int
}
alice := person{
	name: "alice",
	age:  15,
}

_, _ = dl.FInfo(os.Stdout, alice)
Output:

[DeLog] info: dl_test.person{name:"alice", age:15} (dl_test.person) log_example_test.go:50

func Fprintf

func Fprintf(w io.Writer, format string, v ...any) (int, error)

Fprintf formats according to a format specifier and writes to w. Arguments are handled in the manner of fmt.FPrintf.

Example
type person struct {
	name string
	age  int
}
alice := person{
	name: "alice",
	age:  15,
}

dl.Fprintf(os.Stdout, "name: %s", alice.name)
Output:

name: alice

func Fprintln

func Fprintln(w io.Writer, v ...any) (int, error)

Fprintln formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended. Arguments are handled in the manner of fmt.FPrintln.

Example
type person struct {
	name string
	age  int
}
alice := person{
	name: "alice",
	age:  15,
}

dl.Fprintln(os.Stdout, "name:", alice.name)
Output:

name: alice

func Info

func Info[T any](v T) (int, error)

Info gives a val, a type, a file name, a line number to print to the standard logger.

Example
type person struct {
	name string
	age  int
}
alice := person{
	name: "alice",
	age:  15,
}

// dl.Info prints to sandard error.
// [DeLog] info: dl_test.person{name:"alice", age:15} (dl_test.person) log_example_test.go:96
_, _ = dl.Info(alice)
Output:

func Printf

func Printf(format string, v ...any) (int, error)

Printf calls Fprintf to print to the standard logger. Arguments are handled in the manner of fmt.Printf.

Example
type person struct {
	name string
	age  int
}
alice := person{
	name: "alice",
	age:  15,
}

// dl.Printf prints to sandard error.
// name: alice
dl.Printf("name: %s", alice.name)
Output:

func Println

func Println(v ...any) (int, error)

Println calls Fprintln to print to the standard logger. Arguments are handled in the manner of fmt.Printf.

Example
type person struct {
	name string
	age  int
}
alice := person{
	name: "alice",
	age:  15,
}

// dl.Printf prints to sandard error.
// name: alice
//
dl.Println("name:", alice.name)
Output:

Types

type CLI

type CLI struct {
}

CLI structs.

func New

func New() *CLI

New for running dl package with CLI.

func (*CLI) Run

func (d *CLI) Run(ctx context.Context, version string, args []string) error

Run executes each method for dl package.

type Logger

type Logger struct {
	*logr.Logger
}

Logger is a struct for preserving *logr.Logger.

func NewLogger

func NewLogger(l *logr.Logger) *Logger

NewLogger wraps logr.Logger.

Example
zapLog, err := zap.NewDevelopment()
if err != nil {
	panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
var log logr.Logger = zapr.NewLogger(zapLog)

// You can use your logr.Logger as it is.
dlr := dl.NewLogger(&log)

num := 57
dlr.Info("Info: ", "num", num)
Output:

Directories

Path Synopsis
cmd
dl

Jump to

Keyboard shortcuts

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