Skip to content

Commit

Permalink
feat(logger): adding a critical and fetal error verification
Browse files Browse the repository at this point in the history
  • Loading branch information
bgcicca committed Oct 25, 2024
1 parent fbf1b75 commit fba2893
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 36 deletions.
87 changes: 61 additions & 26 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
## Changelog

### GopherLight v0.1 Release Notes
🚀 GopherLight v0.1 - The First Release!
We are excited to announce the first version of express-go, a micro-framework inspired by Express.js, now for the Go universe! This initial release brings the simplicity and flexibility you already love from Express, but with the robust performance and reliability of Go.
### GopherLight v0.3 Relase Notes
🚀 GopherLight v0.3 - Improved routes and added plugin support.

🎯 Main Features
* Simple Routing: Set up routes quickly and easily. Just define the URL and a handler, and that's it!
We are excited to announce the second version of GopherLight, bringing more flexibility and features to our lightweight Go framework. With this update, you’ll experience a more powerful routing system and the addition of convenient plugin support to streamline your development process.

```go
app.Route("/hello", func(r *req.Request, w *req.Response) {
w.Send("Hello, World!")
})
```
### 🎯 Additions

* support for plugins that can personalize your experience developing using GopherLight
* support more detailed error logs

* Easy Request Handling: The framework encapsulates HTTP request details to facilitate access to query parameters, headers and request body.
```go
name := r.QueryParam("name")
token := r.Header("Authorization")
```
type MyPlugin struct{}

* Flexible Responses: Send responses in plain text or JSON, all with convenient methods.
```go
w.Send("Hello, Go!")
w.JSON(map[string]string{"message": "Hello, JSON"})
func (p *MyPlugin) Register(route func(method string, path string, handler func(req *req.Request, res *req.Response))) {
route("GET", "/hello", func(req *req.Request, res *req.Response) {
res.Send("Hello from MyPlugin!")
})
}
```

🛠️ What's Coming
This is only version 0.1, so there's still a lot to go! We are working on:
### 🛠️ Improvements

* Middleware for handling authentication and validations.
* Support dynamic routes and route parameters.
* Improvements in error management and customized responses.
* Improvement in routes, with support for standard http methods such as get, put, post, delete.

📝 Contributions
This is an early version, and we are open to suggestions, improvements and contributions from the community. Feel free to explore the code, open issues, or submit PRs to make express-go even better!
```go
app.Get("/hello", func(r *req.Request, w *req.Response) {
w.Send("Hello, World!")
})
```

### 🚀 What’s Next?
* middleware (authentication, ~~timeout~~, anti csrf, ~~logging~~, etc...)
* next func
* More complete documentation

### GopherLight v0.2 Release Notes
🚀 GopherLight v0.2 - Enhanced Routing and Middleware Support!
Expand Down Expand Up @@ -71,4 +71,39 @@ app.Use(middleware.TimeoutMiddleware(2 * time.Second))
* Enhanced error handling and better integration with third-party services.
Middleware for authentication and CSRF protection.

* 📝 Contributions GopherLight continues to grow with your support! We welcome contributions, suggestions, and improvements from the community. Feel free to explore, submit issues, or open PRs to make the framework even better.
* 📝 Contributions GopherLight continues to grow with your support! We welcome contributions, suggestions, and improvements from the community. Feel free to explore, submit issues, or open PRs to make the framework even better.

### GopherLight v0.1 Release Notes
🚀 GopherLight v0.1 - The First Release!
We are excited to announce the first version of express-go, a micro-framework inspired by Express.js, now for the Go universe! This initial release brings the simplicity and flexibility you already love from Express, but with the robust performance and reliability of Go.

🎯 Main Features
* Simple Routing: Set up routes quickly and easily. Just define the URL and a handler, and that's it!

```go
app.Route("/hello", func(r *req.Request, w *req.Response) {
w.Send("Hello, World!")
})
```

* Easy Request Handling: The framework encapsulates HTTP request details to facilitate access to query parameters, headers and request body.
```go
name := r.QueryParam("name")
token := r.Header("Authorization")
```

* Flexible Responses: Send responses in plain text or JSON, all with convenient methods.
```go
w.Send("Hello, Go!")
w.JSON(map[string]string{"message": "Hello, JSON"})
```

🛠️ What's Coming
This is only version 0.1, so there's still a lot to go! We are working on:

* Middleware for handling authentication and validations.
* Support dynamic routes and route parameters.
* Improvements in error management and customized responses.

📝 Contributions
This is an early version, and we are open to suggestions, improvements and contributions from the community. Feel free to explore the code, open issues, or submit PRs to make express-go even better!
26 changes: 26 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logger
import (
"log"
"net/http"
"os"
"time"
)

Expand All @@ -18,7 +19,32 @@ func LogError(message string) {
log.Printf("[ERROR] %s", message)
}

func LogCriticalError(message string) {
log.Printf("[CRITICAL] %s", message)
}

func LogFatalError(message string) {
log.Fatalf("[FATAL] %s", message)
}

func LogDebug(message string) {
log.Printf("[DEBUG] %s", message)
}

func LogRequest(r *http.Request, status int, duration time.Duration) {
log.Printf("[REQUEST] Method: %s | Path: %s | Status: %d | Duration: %v | User-Agent: %s",
r.Method, r.URL.Path, status, duration, r.UserAgent())
}

func CheckCriticalError(err error, context string) {
if err != nil {
log.Printf("[CRITICAL] %s: %v", context, err)
}
}

func CheckFatalError(err error, context string) {
if err != nil {
log.Fatalf("[FATAL] %s: %v", context, err)
os.Exit(1)
}
}
21 changes: 11 additions & 10 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logger

import (
"bytes"
"errors"
"log"
"testing"
)
Expand All @@ -10,39 +11,39 @@ func captureOutput(f func()) string {
var buf bytes.Buffer
log.SetOutput(&buf)
log.SetFlags(0)
f() // Leave it here because otherwise the logger will try to write to a null pointer, resulting in an "invalid memory address or nil pointer dereference" error.
f()
log.SetOutput(nil)
return buf.String()
}

func TestLogInfo(t *testing.T) {
func TestLogCriticalError(t *testing.T) {
output := captureOutput(func() {
LogInfo("Testing info message")
LogCriticalError("Critical error occurred")
})

expected := "[INFO] Testing info message\n"
expected := "[CRITICAL] Critical error occurred\n"
if output != expected {
t.Errorf("Expected '%s' but got '%s'", expected, output)
}
}

func TestLogWarning(t *testing.T) {
func TestCheckCriticalError(t *testing.T) {
output := captureOutput(func() {
LogWarning("Testing warning message")
CheckCriticalError(errors.New("Critical connection error"), "Database connection")
})

expected := "[WARNING] Testing warning message\n"
expected := "[CRITICAL] Database connection: Critical connection error\n"
if output != expected {
t.Errorf("Expected '%s' but got '%s'", expected, output)
}
}

func TestLogError(t *testing.T) {
func TestLogDebug(t *testing.T) {
output := captureOutput(func() {
LogError("Testing error message")
LogDebug("Debugging application")
})

expected := "[ERROR] Testing error message\n"
expected := "[DEBUG] Debugging application\n"
if output != expected {
t.Errorf("Expected '%s' but got '%s'", expected, output)
}
Expand Down

0 comments on commit fba2893

Please sign in to comment.