Skip to content

Commit

Permalink
refactor: Fix linting errors and add pkg lvl comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-mesiab authored and kmesiab committed Jan 9, 2024
1 parent 0257c4a commit 6912e2a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 29 deletions.
51 changes: 51 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
run:
tests: true # Lint everything
skip-dirs: [] # Lint everything
skip-files: [] # Lint everything
go: 1.21.1

issues:
exclude-use-default: false # Do not use default excludes

disable-all: true # Disable all linters, then enable only ones we want.
linters:
enable:
- asasalint # Check for pass []any as any in variadic func(...any).
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers.
- bodyclose # Checks whether HTTP response body is closed successfully
- containedctx # Containedctx is a linter that detects struct contained context.Context field.
- contextcheck # Check whether the function uses a non-inherited context.
- cyclop # Checks function and package cyclomatic complexity.
# - decorder # Don't use this because we cannot disable for tests, and then you must have fixtures above other things
- dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f()).
- errcheck # Check for unchecked errors
- exportloopref # Checks for pointers to enclosing loop variables
- forcetypeassert # Finds forced type assertions
- goconst # Finds repeated strings that could be replaced by a constant
- gocritic # The most opinionated Go source code linter for code audit
- gosec # Security-related linters
- gosimple # Linter for Go source code that specializes in simplifying code
- govet # Vet examines Go source code to report suspicious constructs
- ineffassign # Detects when assignments to existing variables are not used
- misspell # Correct commonly misspelled English words
- noctx # Checks that http.Request.WithContext is used with incoming requests
- nolintlint # Reports ill-formed or insufficient nolint directives
- prealloc # Finds slice declarations that could potentially be preallocated
- prealloc # Finds slice declarations that could potentially be preallocated
- predeclared # Finds code that shadows one of Go's predeclared identifiers
- reassign # Finds slice reassignment
- sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed
- staticcheck # A collection of modern Go linters
- stylecheck # Stylecheck is a replacement for golint
- unused # Checks Go code for unused constants, variables, functions and types
- varnamelen # Checks that variable and function name lengths are consistent
- wastedassign # Finds wasted assignment statements

linters-settings:
dogsled:
max-blank-identifiers: 1
errcheck:
check-type-assertions: true # Report about not checking of errors in type assertions: `a := b.(MyStruct)`.
check-blank: true # Report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`.
gofmt:
simplify: true # Have gofmt simplify code for us
46 changes: 17 additions & 29 deletions klogger.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
/*
MIT License
Copyright (c) 2024 Kevin Mesiab
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package go_klogger
// Package goklogger provides an enhanced logging experience by wrapping the
// popular logrus package.
//
// This package offers a more structured and flexible
// approach to logging, allowing developers to easily integrate and customize
// logging functionality in their Go applications.
// Key features include the
// ability to set global log levels, add default fields to all log messages,
// and create log messages with various severity levels.
// The package is designed
// to be intuitive and easy to use, while providing powerful capabilities for
// detailed and informative logging.
package goklogger

import (
"fmt"
Expand Down Expand Up @@ -78,21 +66,21 @@ func SetDefaultFields(fields map[string]interface{}) {
}

// Logf creates a new logger with the given format and arguments.
func Logf(format string, a ...interface{}) *KLogger {
func Logf(format string, vars ...interface{}) *KLogger {

// Set up a global logger with default preferences. This is
// only ever done once, whether here or by calling InitializeGlobalLogger
// directly.
InitializeGlobalLogger(DefaultLogLevel, &logrus.JSONFormatter{})

// Pass back an instance of a KLogger with the global logger and default properties.
k := &KLogger{
newLogger := &KLogger{
Logger: globalLogger,
Message: fmt.Sprintf(format, a...),
Message: fmt.Sprintf(format, vars...),
Data: make(map[string]interface{}),
}

return k.AddData(defaultFields)
return newLogger.AddData(defaultFields)
}

// SetLogLevel sets the log level of the logger
Expand Down

0 comments on commit 6912e2a

Please sign in to comment.