-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fa3734e
Showing
14 changed files
with
655 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
/vendor/ | ||
/Godeps/ | ||
|
||
# Directory Generated by air | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Changelog | ||
|
||
## [v1.0.0] - `2024-09-13` | ||
|
||
- Updated dependencies version | ||
- Transfer project to `owlsome-official` | ||
|
||
[v1.0.0]: https://github.com/owlsome-official/sensitive/releases/tag/v1.0.0 | ||
|
||
## v0 is Legacy version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# zlogtime | ||
|
||
[![made-with-Go](https://img.shields.io/badge/Made%20with-Go-1f425f.svg)](http://golang.org) | ||
|
||
zlogtime is a logging library, which measure the elapsed time of services on-demand. | ||
|
||
## Table of Contents | ||
|
||
- [zlogtime](#zlogtime) | ||
- [Table of Contents](#table-of-contents) | ||
- [Installation](#installation) | ||
- [Signatures](#signatures) | ||
- [Examples](#examples) | ||
- [Config](#config) | ||
- [Default Config](#default-config) | ||
- [Dependencies](#dependencies) | ||
- [Example Usage](#example-usage) | ||
|
||
## Installation | ||
|
||
```bash | ||
go get -u github.com/owlsome-official/zlogtime | ||
``` | ||
|
||
## Signatures | ||
|
||
```go | ||
var timeTracker zlogtime.ZLogTime = zlogtime.New() | ||
``` | ||
|
||
or with configuration | ||
|
||
```go | ||
var timeTrackerWithConfig zlogtime.ZLogTime = zlogtime.New( | ||
zlogtime.Config{ | ||
LogLevel: zerolog.DebugLevel.String() | ||
} | ||
) | ||
``` | ||
|
||
## Examples | ||
|
||
```go | ||
// Step 1: Defined as global (tools) variable | ||
var ( | ||
timeTracker zlogtime.ZLogTime = zlogtime.New() | ||
) | ||
|
||
// Step 2: Call TimeTrack within a function | ||
func FuncName() { | ||
// NOTE: Always used with "defer" | ||
defer timeTracker.TimeTrack("NAME", time.Now()) | ||
... | ||
} | ||
``` | ||
|
||
## Config | ||
|
||
```go | ||
type Config struct { | ||
|
||
// Optional. Default: false | ||
Hidden bool | ||
|
||
// Optional. Default: "info" | ||
LogLevel string | ||
|
||
// Optional. Default: "milli". Possible Value: ["nano", "micro", "milli"] | ||
ElapsedTimeUnit string | ||
} | ||
``` | ||
|
||
## Default Config | ||
|
||
```go | ||
var ConfigDefault = Config{ | ||
Hidden: false, | ||
LogLevel: "info", | ||
ElapsedTimeUnit: "milli", | ||
} | ||
``` | ||
|
||
## Dependencies | ||
|
||
- [Zerolog](https://github.com/rs/zerolog) | ||
|
||
## Example Usage | ||
|
||
Please go to [example/main.go](./example/main.go) | ||
|
||
> Made with ❤️ by watsize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package zlogtime | ||
|
||
type Config struct { | ||
|
||
// Optional. Default: false | ||
Hidden bool | ||
|
||
// Optional. Default: "info" | ||
LogLevel string | ||
|
||
// Optional. Default: "milli". Possible Value: ["nano", "micro", "milli"] | ||
ElapsedTimeUnit string | ||
} | ||
|
||
var ConfigDefault = Config{ | ||
Hidden: false, | ||
LogLevel: "info", | ||
ElapsedTimeUnit: "milli", | ||
} | ||
|
||
func configDefault(config ...Config) ZLogTime { | ||
// Return default config if nothing provided | ||
if len(config) < 1 { | ||
return &ConfigDefault | ||
} | ||
|
||
// Override default config | ||
cfg := config[0] | ||
|
||
// Set default values | ||
// Note: cfg.Hidden: it's false by default. | ||
|
||
if cfg.LogLevel == "" { | ||
cfg.LogLevel = ConfigDefault.LogLevel | ||
} | ||
|
||
if cfg.ElapsedTimeUnit == "" { | ||
cfg.ElapsedTimeUnit = ConfigDefault.ElapsedTimeUnit | ||
} | ||
|
||
return &cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
### Just testing... | ||
GET http://localhost:8000/get_data_from_db HTTP/1.1 | ||
|
||
### GET 1-second service | ||
GET http://localhost:8001/one HTTP/1.1 | ||
|
||
### GET 1-second with a half service | ||
GET http://localhost:8001/onepointfive HTTP/1.1 | ||
|
||
### GET 3-second service | ||
GET http://localhost:8001/three HTTP/1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module example | ||
|
||
go 1.22.1 | ||
|
||
require ( | ||
github.com/gofiber/fiber/v2 v2.52.5 | ||
github.com/owlsome-official/zlogtime v0.0.0 | ||
github.com/rs/zerolog v1.33.0 | ||
) | ||
|
||
require ( | ||
github.com/andybalholm/brotli v1.1.0 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/klauspost/compress v1.17.9 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/mattn/go-runewidth v0.0.16 // indirect | ||
github.com/rivo/uniseg v0.4.7 // indirect | ||
github.com/valyala/bytebufferpool v1.0.0 // indirect | ||
github.com/valyala/fasthttp v1.55.0 // indirect | ||
github.com/valyala/tcplisten v1.0.0 // indirect | ||
golang.org/x/sys v0.25.0 // indirect | ||
) | ||
|
||
replace github.com/owlsome-official/zlogtime => ../ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= | ||
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= | ||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= | ||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= | ||
github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= | ||
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= | ||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= | ||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= | ||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= | ||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= | ||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= | ||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= | ||
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | ||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= | ||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= | ||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= | ||
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= | ||
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= | ||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= | ||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= | ||
github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8= | ||
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM= | ||
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= | ||
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= | ||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/owlsome-official/zlogtime" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
var ( | ||
timeTracker zlogtime.ZLogTime = zlogtime.New() | ||
timeTrackerWithConfig zlogtime.ZLogTime = zlogtime.New(zlogtime.Config{LogLevel: zerolog.DebugLevel.String()}) | ||
) | ||
|
||
func main() { | ||
app := fiber.New(fiber.Config{DisableStartupMessage: true}) | ||
|
||
app.Get("/get_data_from_db", Handler) // GET http://localhost:8000/get_data_from_db | ||
|
||
fmt.Println("Listening on http://localhost:8000") | ||
fmt.Println("Try to send a request :D") | ||
go app.Listen(":8000") | ||
|
||
customApp := fiber.New(fiber.Config{DisableStartupMessage: true}) | ||
customApp.Get("/one", HandlerBySecond(1)) // GET http://localhost:8000/one | ||
customApp.Get("/onepointfive", HandlerBySecond(1.5)) // GET http://localhost:8000/onepointfive | ||
customApp.Get("/three", HandlerBySecond(3)) // GET http://localhost:8000/three | ||
customApp.Listen(":8001") | ||
} | ||
|
||
func Handler(c *fiber.Ctx) error { | ||
StopFor(0.02) | ||
return c.SendString("It's took 20ms!") | ||
} | ||
|
||
func StopFor(sec float64) { | ||
defer timeTracker.TimeTrack("Stop for "+fmt.Sprintf("%v", sec)+"s", time.Now()) | ||
time.Sleep(time.Duration(sec*1000) * time.Millisecond) | ||
} | ||
|
||
// =========================== | ||
// CUSTOM APP WITH DEBUG LEVEL | ||
// =========================== | ||
func HandlerBySecond(sec float64) fiber.Handler { | ||
return func(c *fiber.Ctx) error { | ||
DebugStopFor(sec) | ||
return c.SendString("Watch your app logs!") | ||
} | ||
} | ||
|
||
func DebugStopFor(sec float64) { | ||
defer timeTrackerWithConfig.TimeTrack("Stop for "+fmt.Sprintf("%v", sec)+"s", time.Now()) | ||
time.Sleep(time.Duration(sec*1000) * time.Millisecond) | ||
} | ||
|
||
// =========================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module github.com/owlsome-official/zlogtime | ||
|
||
go 1.22.1 | ||
|
||
require github.com/rs/zerolog v1.33.0 | ||
|
||
require ( | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
golang.org/x/sys v0.25.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= | ||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= | ||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= | ||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= | ||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= | ||
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0= | ||
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= | ||
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= | ||
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= | ||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= | ||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package zlogtime | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
var ( | ||
TimeFieldFormat string = time.RFC3339Nano | ||
) | ||
|
||
func init() { | ||
zerolog.TimeFieldFormat = TimeFieldFormat | ||
zerolog.TimestampFieldName = "timestamp" | ||
} | ||
|
||
func New(conf ...Config) ZLogTime { | ||
// set default config | ||
cfg := configDefault(conf...) | ||
|
||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package zlogtime | ||
|
||
import ( | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type ZLogTime interface { | ||
TimeTrack(name string, start time.Time) | ||
} | ||
|
||
func (cfg *Config) TimeTrack(name string, start time.Time) { | ||
eventName := "TimeTrack" | ||
if name != "" { | ||
eventName = name | ||
} | ||
|
||
logTime := time.Now() | ||
elapsedTime := getTimeDuration(time.Since(start), cfg.ElapsedTimeUnit) | ||
shortUnit := getShortDurationUnit(cfg.ElapsedTimeUnit) | ||
|
||
logger := getLogLevel(cfg.LogLevel) | ||
logger = logger. | ||
Interface("event", map[string]interface{}{ | ||
"name": eventName, | ||
"start": start.Format(TimeFieldFormat), | ||
"end": logTime.Format(TimeFieldFormat), | ||
"elapsed_time": elapsedTime, | ||
"elapsed_time_unit": cfg.ElapsedTimeUnit, | ||
"elapsed_time_unit_short": shortUnit, | ||
"elapsed_time_string": strconv.FormatInt(elapsedTime, 10) + shortUnit, | ||
}) | ||
|
||
logger.Msg("TimeTrack at: " + name) | ||
} |
Oops, something went wrong.