From 41f6e8963a0adb12f39e6cd6556df0a071746665 Mon Sep 17 00:00:00 2001 From: Jude Dike Date: Sun, 5 Jan 2020 16:49:44 +0100 Subject: [PATCH 1/8] feat: Log JSON format --- .github/PULL_REQUEST_TEMPLATE.md | 9 +++++++ example/json.go | 26 ++++++++++++++++++ logs/error.json | 1 + logs/error.log | 6 ++--- thoth.go | 45 +++++++++++++++++++++++++++++++- 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 example/json.go create mode 100644 logs/error.json diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..6707d7f --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,9 @@ +#### What does this PR do + +#### Description of Task to be completed + +#### How should this be manually tested + +#### Any background context you want to add + +#### Screenshots diff --git a/example/json.go b/example/json.go new file mode 100644 index 0000000..75c8ced --- /dev/null +++ b/example/json.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "github.com/ichtrojan/thoth" + "log" + "net/http" +) + +func main() { + logger := thoth.InitJson() + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + _, err := fmt.Fprintf(w, "Hello, Testing from Thoth") + + if err != nil { + log.Fatal(err) + } + + fmt.Println("Endpoint served") + }) + + if err := http.ListenAndServe(":8888", nil); err != nil { + logger.LogJson(err) + } +} diff --git a/logs/error.json b/logs/error.json new file mode 100644 index 0000000..7309803 --- /dev/null +++ b/logs/error.json @@ -0,0 +1 @@ +[{"error":"listen tcp :8888: bind: address already in use","timestamp":"2020-01-05 16:46:15"}] \ No newline at end of file diff --git a/logs/error.log b/logs/error.log index 196c067..f38def7 100644 --- a/logs/error.log +++ b/logs/error.log @@ -1,3 +1,3 @@ -[2020-01-05 14:35:36] listen tcp :8888: bind: address already in use -[2020-01-05 14:35:39] listen tcp :8888: bind: address already in use -[2020-01-05 14:35:41] listen tcp :8888: bind: address already in use +[2020-01-05 16:37:23] listen tcp :8888: bind: address already in use +[2020-01-05 16:45:37] listen tcp :8888: bind: address already in use +[2020-01-05 16:47:06] listen tcp :8888: bind: address already in use diff --git a/thoth.go b/thoth.go index 6e32a83..47d6d5a 100644 --- a/thoth.go +++ b/thoth.go @@ -4,6 +4,8 @@ import ( "fmt" "os" "time" + "encoding/json" + "io/ioutil" ) const directory = "logs" @@ -38,6 +40,24 @@ func Init() Config { return Config{directory: path} } +func InitJson(params ...string) Config { + path := fmt.Sprintf("%s/error.json", directory) + + var _, err = os.Stat(path) + + if os.IsNotExist(err) { + file, err := os.Create(path) + + if err != nil { + fmt.Println(err) + } + + defer file.Close() + } + + return Config{directory: path} +} + func (config Config) Log(error error) { path := config.directory @@ -49,7 +69,7 @@ func (config Config) Log(error error) { defer file.Close() - newError := fmt.Sprintf("[%s] %s", time.Now().Format("2006-01-02 15:04:05"), error) + newError := fmt.Sprintf("[%s] %s", time.Now().Format("2006-01-02 15:04:05"), error.Error()) _, err = fmt.Fprintln(file, newError) @@ -65,3 +85,26 @@ func (config Config) Log(error error) { return } + +func (config Config) LogJson(error error) { + path := config.directory + + var file, err = ioutil.ReadFile(path) + + if err != nil { + fmt.Println(err) + } + jsonData := []map[string]interface{}{} + json.Unmarshal(file, &jsonData) + + newError := map[string]interface{}{ + "timestamp": time.Now().Format("2006-01-02 15:04:05"), + "error": error.Error(), + } + + jsonData = append(jsonData, newError) + jsonString, _ := json.Marshal(jsonData) + + ioutil.WriteFile(path, jsonString, os.ModePerm) + return +} From 157cc39ce0277e24be19cdb703746c35710ff64e Mon Sep 17 00:00:00 2001 From: Jude Dike Date: Mon, 6 Jan 2020 13:14:37 +0100 Subject: [PATCH 2/8] fix: combined json and log to Init() --- example/json.go | 5 +++-- example/main.go | 6 +++--- logs/error.json | 1 - thoth.go | 29 ++++++++--------------------- 4 files changed, 14 insertions(+), 27 deletions(-) diff --git a/example/json.go b/example/json.go index 75c8ced..15cb2ea 100644 --- a/example/json.go +++ b/example/json.go @@ -2,13 +2,14 @@ package main import ( "fmt" - "github.com/ichtrojan/thoth" "log" "net/http" + + "github.com/ichtrojan/thoth" ) func main() { - logger := thoth.InitJson() + logger := thoth.Init("json") http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { _, err := fmt.Fprintf(w, "Hello, Testing from Thoth") diff --git a/example/main.go b/example/main.go index fa11263..fd63695 100644 --- a/example/main.go +++ b/example/main.go @@ -2,14 +2,14 @@ package main import ( "fmt" - "github.com/ichtrojan/thoth" "log" "net/http" + + "github.com/ichtrojan/thoth" ) func main() { - logger := thoth.Init() - + logger := thoth.Init("log") http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { _, err := fmt.Fprintf(w, "Hello, Testing from Thoth") diff --git a/logs/error.json b/logs/error.json index 7309803..e69de29 100644 --- a/logs/error.json +++ b/logs/error.json @@ -1 +0,0 @@ -[{"error":"listen tcp :8888: bind: address already in use","timestamp":"2020-01-05 16:46:15"}] \ No newline at end of file diff --git a/thoth.go b/thoth.go index 47d6d5a..07a5dc3 100644 --- a/thoth.go +++ b/thoth.go @@ -1,11 +1,11 @@ package thoth import ( + "encoding/json" "fmt" + "io/ioutil" "os" "time" - "encoding/json" - "io/ioutil" ) const directory = "logs" @@ -14,7 +14,7 @@ type Config struct { directory string } -func Init() Config { +func Init(filetype string) Config { if _, err := os.Stat(directory); os.IsNotExist(err) { err = os.MkdirAll(directory, 0755) @@ -23,25 +23,12 @@ func Init() Config { } } - path := fmt.Sprintf("%s/error.log", directory) - - var _, err = os.Stat(path) - - if os.IsNotExist(err) { - file, err := os.Create(path) - - if err != nil { - fmt.Println(err) - } - - defer file.Close() + var filename = "error.log" + if filetype == "json" { + filename = "error.json" } - return Config{directory: path} -} - -func InitJson(params ...string) Config { - path := fmt.Sprintf("%s/error.json", directory) + path := fmt.Sprintf("%s/%s", directory, filename) var _, err = os.Stat(path) @@ -99,7 +86,7 @@ func (config Config) LogJson(error error) { newError := map[string]interface{}{ "timestamp": time.Now().Format("2006-01-02 15:04:05"), - "error": error.Error(), + "error": error.Error(), } jsonData = append(jsonData, newError) From f2061f37ad7747d2e7d5cb86002959637ffefae9 Mon Sep 17 00:00:00 2001 From: Jude Dike Date: Mon, 6 Jan 2020 13:16:41 +0100 Subject: [PATCH 3/8] fix: changed timestamp to unix --- thoth.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thoth.go b/thoth.go index 07a5dc3..41dff42 100644 --- a/thoth.go +++ b/thoth.go @@ -85,7 +85,7 @@ func (config Config) LogJson(error error) { json.Unmarshal(file, &jsonData) newError := map[string]interface{}{ - "timestamp": time.Now().Format("2006-01-02 15:04:05"), + "timestamp": time.Now().Unix(), "error": error.Error(), } From 9fa216f363880e589327f1f000d0fbf0c59a906d Mon Sep 17 00:00:00 2001 From: ichtrojan Date: Wed, 8 Jan 2020 11:20:48 +0100 Subject: [PATCH 4/8] Refactor for extention --- thoth.go | 63 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/thoth.go b/thoth.go index 41dff42..304c2ff 100644 --- a/thoth.go +++ b/thoth.go @@ -2,6 +2,7 @@ package thoth import ( "encoding/json" + "errors" "fmt" "io/ioutil" "os" @@ -12,20 +13,29 @@ const directory = "logs" type Config struct { directory string + filetype string } -func Init(filetype string) Config { +func Init(filetype string) (Config, error) { + var config Config + if _, err := os.Stat(directory); os.IsNotExist(err) { err = os.MkdirAll(directory, 0755) if err != nil { - fmt.Println(err) + return config, err } } - var filename = "error.log" - if filetype == "json" { + var filename string + + switch filetype { + case "log": + filename = "error.log" + case "json": filename = "error.json" + default: + return config, errors.New("adapter not defined") } path := fmt.Sprintf("%s/%s", directory, filename) @@ -42,16 +52,27 @@ func Init(filetype string) Config { defer file.Close() } - return Config{directory: path} + return Config{directory: path, filetype: filetype}, nil } func (config Config) Log(error error) { + switch config.filetype { + case "log": + _ = config.logFile(error) + case "json": + _ = config.logJson(error) + default: + return + } +} + +func (config Config) logFile(error error) error { path := config.directory var file, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644) if err != nil { - fmt.Println(err) + return err } defer file.Close() @@ -61,37 +82,45 @@ func (config Config) Log(error error) { _, err = fmt.Fprintln(file, newError) if err != nil { - fmt.Println(err) + return err } err = file.Sync() if err != nil { - fmt.Println(err) + return err } - return + return nil } -func (config Config) LogJson(error error) { +func (config Config) logJson(error error) error { path := config.directory var file, err = ioutil.ReadFile(path) if err != nil { - fmt.Println(err) + return err } - jsonData := []map[string]interface{}{} - json.Unmarshal(file, &jsonData) + + var jsonData []map[string]interface{} + + _ = json.Unmarshal(file, &jsonData) newError := map[string]interface{}{ - "timestamp": time.Now().Unix(), + "timestamp": time.Now().Format("2006-01-02 15:04:05"), "error": error.Error(), } jsonData = append(jsonData, newError) - jsonString, _ := json.Marshal(jsonData) - ioutil.WriteFile(path, jsonString, os.ModePerm) - return + jsonString, err := json.Marshal(jsonData) + + if err != nil { + return err + } + + _ = ioutil.WriteFile(path, jsonString, os.ModePerm) + + return nil } From 7b242c2e75cead703230188a31f50b53f525c292 Mon Sep 17 00:00:00 2001 From: ichtrojan Date: Wed, 8 Jan 2020 11:21:11 +0100 Subject: [PATCH 5/8] Ignored log directory --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6226bf4..ed5921d 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,7 @@ # vendor/ # Goland -/.idea \ No newline at end of file +/.idea + +# Testing Log Directory +/logs \ No newline at end of file From dd09ec103fc440d08dffd583c361a47f1fb6a3a0 Mon Sep 17 00:00:00 2001 From: ichtrojan Date: Wed, 8 Jan 2020 11:21:23 +0100 Subject: [PATCH 6/8] Deleted Log directory --- logs/error.json | 0 logs/error.log | 3 --- 2 files changed, 3 deletions(-) delete mode 100644 logs/error.json delete mode 100644 logs/error.log diff --git a/logs/error.json b/logs/error.json deleted file mode 100644 index e69de29..0000000 diff --git a/logs/error.log b/logs/error.log deleted file mode 100644 index f38def7..0000000 --- a/logs/error.log +++ /dev/null @@ -1,3 +0,0 @@ -[2020-01-05 16:37:23] listen tcp :8888: bind: address already in use -[2020-01-05 16:45:37] listen tcp :8888: bind: address already in use -[2020-01-05 16:47:06] listen tcp :8888: bind: address already in use From c8f6b8847e9d731512ac9a06cdf4629c6c63c22e Mon Sep 17 00:00:00 2001 From: ichtrojan Date: Wed, 8 Jan 2020 11:21:40 +0100 Subject: [PATCH 7/8] Refactored example file --- example/json.go | 27 --------------------------- example/main.go | 19 +++++++++++++++---- 2 files changed, 15 insertions(+), 31 deletions(-) delete mode 100644 example/json.go diff --git a/example/json.go b/example/json.go deleted file mode 100644 index 15cb2ea..0000000 --- a/example/json.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import ( - "fmt" - "log" - "net/http" - - "github.com/ichtrojan/thoth" -) - -func main() { - logger := thoth.Init("json") - - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - _, err := fmt.Fprintf(w, "Hello, Testing from Thoth") - - if err != nil { - log.Fatal(err) - } - - fmt.Println("Endpoint served") - }) - - if err := http.ListenAndServe(":8888", nil); err != nil { - logger.LogJson(err) - } -} diff --git a/example/main.go b/example/main.go index fd63695..ea33622 100644 --- a/example/main.go +++ b/example/main.go @@ -2,25 +2,36 @@ package main import ( "fmt" - "log" "net/http" "github.com/ichtrojan/thoth" ) func main() { - logger := thoth.Init("log") + json, err := thoth.Init("json") + + if err != nil { + fmt.Println(err) + } + + file, err := thoth.Init("log") + + if err != nil { + fmt.Println(err) + } + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { _, err := fmt.Fprintf(w, "Hello, Testing from Thoth") if err != nil { - log.Fatal(err) + json.Log(err) } fmt.Println("Endpoint served") }) if err := http.ListenAndServe(":8888", nil); err != nil { - logger.Log(err) + json.Log(err) + file.Log(err) } } From 00b37cc97ff7836f71c03f291a4dde9239b3ed0a Mon Sep 17 00:00:00 2001 From: ichtrojan Date: Wed, 8 Jan 2020 11:26:22 +0100 Subject: [PATCH 8/8] New Readmw --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 1ce4412..1a79cda 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,9 @@ In Egyptian Mythology, Thoth was the Egyptian ibis-headed god of knowledge, magi Thoth is an error logger for go. It helps log errors to a log file so you can go back to find how why and when something breaks in production. +>**NOTE** +>Log file can be either a `.log` file or a `.json` file at the time of this release + # Installation You can install thoth by running