From ade93c1d23d3ab1a1e221018799dd5541792b036 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Tue, 1 Dec 2020 18:00:26 -0800 Subject: [PATCH] Move from dockron --- .gitignore | 1 - .pre-commit-config.yaml | 21 ++++++++++ Makefile | 22 ++++++++++ README.md | 53 +++++++++++++++++++++++- add-docs-to-readme.sh | 8 ++++ go.mod | 3 ++ slog.go | 90 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 Makefile create mode 100755 add-docs-to-readme.sh create mode 100644 go.mod create mode 100644 slog.go diff --git a/.gitignore b/.gitignore index f4d432a..af75d98 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,3 @@ # Dependency directories (remove the comment below to include it) # vendor/ - diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..df99911 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,21 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.4.0 + hooks: + - id: check-added-large-files + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-merge-conflict + - repo: git://github.com/dnephin/pre-commit-golang + rev: v0.3.5 + hooks: + - id: go-fmt + - id: go-imports + # - id: gometalinter + # - id: golangci-lint + - repo: https://github.com/IamTheFij/docker-pre-commit + rev: v2.0.0 + hooks: + - id: docker-compose-check + - id: hadolint-system diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8a8eee5 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +.PHONY: all clean +all: test README.md + +.PHONY: default +default: test + +.PHONY: test +test: check + +# Installs pre-commit hooks +.PHONY: install-hooks +install-hooks: + pre-commit install --install-hooks + +# Runs pre-commit checks on files +.PHONY: check +check: + pre-commit run --all-files + + +README.md: ./add-docs-to-readme.sh *.go + ./add-docs-to-readme.sh diff --git a/README.md b/README.md index 625cbe2..59fe25d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,54 @@ # slog -Simple logger for Go with no dependencies \ No newline at end of file +A simple logger for Go with no dependencies + +I know there are many go loggers out there that offer various logging features such as file rotation, granular verbosity settings, colored and JSON output, etc. + +_Slog is not one of them._ + +Slog lets you hide or show debug logs as well as provides a simpler way to log messages with Warning and Error prefixes for consistency. + +Also provided are a few simple methods for handling returned `error` variables, logging them out and optionally panicing or fatally exiting. + +## Documentation + package slog // import "git.iamthefij.com/iamthefij/slog" + + Package slog is a super simple logger that allows a few convenience methods + for handling debug vs warning/error logs. It also adds a few conveniences + for handling errors. + + VARIABLES + + var ( + // DebugLevel indicates if we should log at the debug level + DebugLevel = true + ) + + FUNCTIONS + + func Debug(format string, v ...interface{}) + Debug will log with a DEBUG prefix if DebugLevel is se + + func Error(format string, v ...interface{}) + Error will log with a ERROR prefix + + func FatalOnErr(err error, format string, v ...interface{}) + FatalOnErr if error provided, will log out details of an error and exi + + func Info(format string, v ...interface{}) + Info formats logs with an INFO prefix + + func Log(format string, v ...interface{}) + Log formats logs directly to the main logger + + func PanicOnErr(err error, format string, v ...interface{}) + PanicOnErr if error provided, will log out details of an error and exi + + func SetFlags(flag int) + SetFlags allows changing the logger flags using flags found in `log` + + func WarnOnErr(err error, format string, v ...interface{}) + WarnOnErr if error provided, will provide a warning if an error is provided + + func Warning(format string, v ...interface{}) + Warning will log with a WARNING prefix diff --git a/add-docs-to-readme.sh b/add-docs-to-readme.sh new file mode 100755 index 0000000..2c2f74d --- /dev/null +++ b/add-docs-to-readme.sh @@ -0,0 +1,8 @@ +#! /bin/bash +set -e + +slogdir=$(dirname "$0") +readme="$slogdir/README.md" + +awk '/## Documentation/ {print ; exit} {print}' "$readme" > "$readme.tmp" && go doc -all slog | sed "s/^/ /;s/[ \t]*$//" >> "$readme.tmp" +mv "$readme.tmp" "$readme" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..be93693 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.iamthefij.com/iamthefij/slog + +go 1.15 diff --git a/slog.go b/slog.go new file mode 100644 index 0000000..fdb5eac --- /dev/null +++ b/slog.go @@ -0,0 +1,90 @@ +// Package slog is a super simple logger that allows a few convenience methods +// for handling debug vs warning/error logs. It also adds a few conveniences for +// handling errors. +package slog + +import ( + "log" + "os" +) + +var ( + // DebugLevel indicates if we should log at the debug level + DebugLevel = true + + // Default set of flags to use + defaultFlags = log.LstdFlags | log.Lmsgprefix + + // Loggers for various levels. Prefixes are padded to align logged content + loggerInfo = log.New(os.Stderr, "INFO ", defaultFlags) + loggerWarning = log.New(os.Stderr, "WARNING ", defaultFlags) + loggerError = log.New(os.Stderr, "ERROR ", defaultFlags) + loggerDebug = log.New(os.Stderr, "DEBUG ", defaultFlags) + + // Convenience for calling functions for all loggers in one method + allLoggers = []*log.Logger{ + loggerInfo, + loggerWarning, + loggerError, + loggerDebug, + } +) + +// SetFlags allows changing the logger flags using flags found in `log` +func SetFlags(flag int) { + for _, logger := range allLoggers { + logger.SetFlags(flag) + } +} + +// Log formats logs directly to the main logger +func Log(format string, v ...interface{}) { + log.Printf(format, v...) +} + +// Info formats logs with an INFO prefix +func Info(format string, v ...interface{}) { + loggerInfo.Printf(format, v...) +} + +// Warning will log with a WARNING prefix +func Warning(format string, v ...interface{}) { + loggerWarning.Printf(format, v...) +} + +// Error will log with a ERROR prefix +func Error(format string, v ...interface{}) { + loggerError.Printf(format, v...) +} + +// Debug will log with a DEBUG prefix if DebugLevel is set +func Debug(format string, v ...interface{}) { + if !DebugLevel { + return + } + loggerDebug.Printf(format, v...) +} + +// WarnOnErr if error provided, will provide a warning if an error is provided +func WarnOnErr(err error, format string, v ...interface{}) { + if err != nil { + loggerWarning.Printf(format, v...) + loggerError.Print(err) + } +} + +// FatalOnErr if error provided, will log out details of an error and exit +func FatalOnErr(err error, format string, v ...interface{}) { + if err != nil { + loggerError.Printf(format, v...) + loggerError.Fatal(err) + } +} + +// PanicOnErr if error provided, will log out details of an error and exit +func PanicOnErr(err error, format string, v ...interface{}) { + if err != nil { + loggerError.Printf(format, v...) + loggerError.Panic(err) + } +}