Skip to content

Commit

Permalink
Optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpsagarpawar committed Jun 26, 2023
1 parent d700cac commit 02c485a
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 144 deletions.
107 changes: 106 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,106 @@
# logger
# Logger Library

The Logger library provides a simple and flexible logging mechanism for Go applications. It allows you to log messages with different log levels, such as debug, info, warning, error, and panic.

## Installation

To use the Logger library in your Go project, you need to import it:

```
go import "github.com/sdpsagarpawar/logger"
```

## Usage
```
logger := logger.NewLogger()
```

## Logging Levels
The Logger library supports the following log levels:

Debug: Used for detailed debugging information.
Info: Used for general information messages.
Warning: Used to indicate potential issues or warnings.
Error: Used to log error messages.
Panic: Used to log critical errors and panic the application.
Fatal: Used to log fatal errors and exit the application

## Logging Methods
The Logger library provides the following logging methods:

Debug(values ...interface{}): Logs a debug message.

Debugf(format string, values ...interface{}): Logs a formatted debug message.

Debugln(values ...interface{}): Logs a debug message with a newline character.

Info(values ...interface{}): Logs an info message.

Infof(format string, values ...interface{}): Logs a formatted info message.

Infoln(values ...interface{}): Logs an info message with a newline character.

Warning(values ...interface{}): Logs a warning message.

Warningf(format string, values ...interface{}): Logs a formatted warning message.

Warningln(values ...interface{}): Logs a warning message with a newline character.

Error(values ...interface{}): Logs an error message.

Errorf(format string, values ...interface{}): Logs a formatted error message.

Errorln(values ...interface{}): Logs an error message with a newline character.

Panic(values ...interface{}): Logs a panic message and panics the application.

Panicf(format string, values ...interface{}): Logs a formatted panic message and panics the application.

Panicln(values ...interface{}): Logs a panic message with a newline character and panics the application.

Fatal(values ...interface{}): Logs a fatal error message and exits the application.

Fatalf(format string, values ...interface{}): Logs a formatted fatal error message and exits the application.

Fatalln(values ...interface{}): Logs a fatal error message with a newline character and exits the application.

Log(values ...interface{}): Logs a log message.

Logf(format string, values ...interface{}): Logs a formatted log message.

Logln(values ...interface{}): Logs a log message with a newline character.

## Example
Here's an example of how to use the Logger library:
```
package main
import (
"github.com/sdpsagarpawar/logger"
)
func main() {
// Create a new logger
logger := logger.NewLogger()
// Log messages
logger.Debug("Debug message")
logger.Info("Info message")
logger.Warning("Warning message")
logger.Error("Error message")
logger.Panic("Panic message")
logger.Fatal("Fatal message")
}
```
This example demonstrates how to create a logger instance and use various logging methods to log messages with different log levels.

## License
This project is licensed under the MIT License.

```
Feel free to customize the README.md file according to your needs, including the installation instructions, usage examples, and licensing information.
```
20 changes: 0 additions & 20 deletions debug.go

This file was deleted.

18 changes: 0 additions & 18 deletions error.go

This file was deleted.

20 changes: 0 additions & 20 deletions fatal.go

This file was deleted.

20 changes: 0 additions & 20 deletions info.go

This file was deleted.

166 changes: 137 additions & 29 deletions log.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,145 @@
package logger

type Logger interface {
Panic(...interface{})
Panicf(string, ...interface{})
Panicln(...interface{})
Info(...interface{})
Infof(string, ...interface{})
Infoln(...interface{})
Error(...interface{})
Errorf(string, ...interface{})
Errorln(...interface{})
Warning(...interface{})
Warningf(string, ...interface{})
Warningln(...interface{})
Fatal(...interface{})
Fatalf(string, ...interface{})
Fatalln(...interface{})
Debug(...interface{})
Debugf(string, ...interface{})
Debugln(...interface{})
}
import (
"log"
"os"
)

// LogLevel represents the log levels
type LogLevel int

const (
info = "INFO: "
panic = "PANIC: "
err = "ERROR: "
warning = "WARNING: "
fatal = "FATAL: "
debug = "DEBUG: "
// LogLevelDebug represents the debug log level
LogLevelDebug LogLevel = iota

// LogLevelInfo represents the info log level
LogLevelInfo

// LogLevelWarning represents the warning log level
LogLevelWarning

// LogLevelError represents the error log level
LogLevelError
)

type logger struct{}
// Logger represents a logger instance
type Logger struct {
debugLogger *log.Logger
infoLogger *log.Logger
warningLogger *log.Logger
errorLogger *log.Logger
}

// NewLogger creates a new logger instance
func NewLogger() *Logger {
logger := &Logger{}

// Create the debug logger
logger.debugLogger = log.New(os.Stdout, "[DEBUG] ", log.Ldate|log.Ltime)

// Create the info logger
logger.infoLogger = log.New(os.Stdout, "[INFO] ", log.Ldate|log.Ltime)

// Create the warning logger
logger.warningLogger = log.New(os.Stdout, "[WARNING] ", log.Ldate|log.Ltime)

// Create the error logger
logger.errorLogger = log.New(os.Stderr, "[ERROR] ", log.Ldate|log.Ltime)

return logger
}

// Debug prints a debug message
func (logger *Logger) Debug(values ...interface{}) {
logger.debugLogger.Println(values...)
}

// Debugf prints a formatted debug message
func (logger *Logger) Debugf(format string, values ...interface{}) {
logger.debugLogger.Printf(format, values...)
}

// Debugln prints a debug message with a newline character
func (logger *Logger) Debugln(values ...interface{}) {
logger.debugLogger.Println(values...)
}

// Info prints an info message
func (logger *Logger) Info(values ...interface{}) {
logger.infoLogger.Println(values...)
}

// Warning prints a warning message
func (logger *Logger) Warning(values ...interface{}) {
logger.warningLogger.Println(values...)
}

// Warningf prints a formatted warning message
func (logger *Logger) Warningf(format string, values ...interface{}) {
logger.warningLogger.Printf(format, values...)
}

// Warningln prints a warning message with a newline character
func (logger *Logger) Warningln(values ...interface{}) {
logger.warningLogger.Println(values...)
}

// Error prints an error message
func (logger *Logger) Error(values ...interface{}) {
logger.errorLogger.Println(values...)
}

// Errorf prints a formatted error message
func (logger *Logger) Errorf(format string, values ...interface{}) {
logger.errorLogger.Printf(format, values...)
}

// Errorln prints an error message with a newline character
func (logger *Logger) Errorln(values ...interface{}) {
logger.errorLogger.Println(values...)
}

// Fatal prints a fatal error message and exits the program
func (logger *Logger) Fatal(values ...interface{}) {
logger.errorLogger.Fatal(values...)
}

// Fatalf prints a formatted fatal error message and exits the program
func (logger *Logger) Fatalf(format string, values ...interface{}) {
logger.errorLogger.Fatalf(format, values...)
}

// Fatalln prints a fatal error message with a newline character and exits the program
func (logger *Logger) Fatalln(values ...interface{}) {
logger.errorLogger.Fatalln(values...)
}

// Log prints a log message
func (logger *Logger) Log(values ...interface{}) {
logger.infoLogger.Println(values...)
}

// Logf prints a formatted log message
func (logger *Logger) Logf(format string, values ...interface{}) {
logger.infoLogger.Printf(format, values...)
}

// Logln prints a log message with a newline character
func (logger *Logger) Logln(values ...interface{}) {
logger.infoLogger.Println(values...)
}

// Panic logs a panic message and panics the application
func (logger *Logger) Panic(values ...interface{}) {
logger.errorLogger.Panic(values...)
}

// Panicf logs a formatted panic message and panics the application
func (logger *Logger) Panicf(format string, values ...interface{}) {
logger.errorLogger.Panicf(format, values...)
}

func NewLogger() Logger {
return &logger{}
// Panicln logs a panic message with a newline character and panics the application
func (logger *Logger) Panicln(values ...interface{}) {
logger.errorLogger.Panicln(values...)
}
18 changes: 0 additions & 18 deletions panic.go

This file was deleted.

Loading

0 comments on commit 02c485a

Please sign in to comment.