Browse Source

Init commit

Joachim M. Giæver 7 years ago
parent
commit
9cfc338e67
6 changed files with 96 additions and 17 deletions
  1. 50 1
      README.md
  2. 14 4
      config/config.go
  3. 12 0
      errors/errors.go
  4. 0 9
      log/errors/errors.go
  5. 4 3
      log/log.go
  6. 16 0
      main.go

+ 50 - 1
README.md

@@ -1,3 +1,52 @@
 # go-log
 
-Make logging a charm and log whatever you want, whenever you want.
+Make logging a charm and log whatever you want, whenever you want.
+
+## Simple usage
+Just include the package `git.giaever.org/joachimmg/go-log.git/log` in the top of your file and start your logging, e.g:
+```go
+log.Tracef("This is a %s-log", "trace")
+log.Infof("This is a %s-log", "info")
+log.Warningf("This is a %s-log", "warning")
+log.Errorf("This is a %s-log", "error")
+log.Panicf("This is a %s-log", "panic")
+```
+
+which gives you an output like
+```
+TRACE 2016/11/25 01:21:24.243863 This is a trace-log
+INFO 2016/11/25 01:21:24.243993 This is a info-log
+WARNING 2016/11/25 01:21:24.244005 This is a warning-log
+ERROR 2016/11/25 01:21:24.244023 This is a error-log
+```
+
+Yes, Panic wont appear here as Error and Panic functions will end the program!
+
+## How to adjust output level?
+Well, you can parse the flags, e.g by importing flags into your application where `log` is used. E.g:
+
+```go
+import (
+    "flag"
+
+    "git.giaever.org/joachimmg/go-log.git/log"
+)
+
+func main() {
+    flag.Parse()
+    log.Trace("Will be shown if flag -llo TRACE is set and be stored into file if -llf TRACE is set")
+}
+```
+
+### Flags?
+Yes, now you can specify to your program what log-level you want
+
+1. Printed to screen: `-llo` (log level output)
+2. Stored to file: `-llf` (log level file)
+
+Example: `./my_app -llo INFO -llf TRACE" will print info, warning, error and panic logs, but not trace. Everyone (including trace logs) will be written to file.
+
+### Nah, I dont want flags in my application.
+Okey, set the variables manually by including the ./config, and call `config.LLO.Set(config.INFO)` or `config.LLF.Set(config.TRACE)`.
+
+Enjoy!

+ 14 - 4
log/config/config.go → config/config.go

@@ -1,20 +1,20 @@
 package config
 
 import (
-	"errors"
 	"io"
 	"io/ioutil"
 	"log"
 	"os"
 	"strings"
 
-	_c "source.uit.com/mikevoets/inf-3200-1/config"
+	"git.giaever.org/joachimmg/go-log.git/errors"
 )
 
 type LogPrefix string
 type LogLevel int
 type LogHandle io.Writer
 type LogFlag int
+type LogDir string
 
 var (
 	Trace   *log.Logger
@@ -31,7 +31,7 @@ var (
 	HANDLE_WARNING LogHandle = os.Stdout
 	HANDLE_ERROR   LogHandle = os.Stderr
 	HANDLE_PANIC   LogHandle = os.Stderr
-	LOG_DIR                  = _c.TmpDir + string(os.PathSeparator) + "logs"
+	LOG_DIR        LogDir    = LogDir(os.TempDir() + string(os.PathSeparator) + "go-log")
 	FILE_EXTRA     string    = ""
 	LOG_FORMAT               = log.Ldate | log.Lmicroseconds
 )
@@ -77,8 +77,9 @@ func (c *LogLevel) Set(l string) error {
 	} else if l == "PANIC" {
 		*c = PANIC
 	} else {
-		return errors.New("Unknown log level: TRACE|INFO|WARNING|ERROR|PANIC")
+		return errors.UNKNOWN_LEVEL
 	}
+
 	return nil
 }
 
@@ -96,3 +97,12 @@ func (c *LogLevel) String() string {
 		return "PANIC"
 	}
 }
+
+func (d *LogDir) Set(nd string) error {
+	LOG_DIR = LogDir(nd)
+	return nil
+}
+
+func (d *LogDir) String() string {
+	return string(*d)
+}

+ 12 - 0
errors/errors.go

@@ -0,0 +1,12 @@
+package errors
+
+import (
+	e "errors"
+)
+
+var (
+	UNKNOWN_LOGGER = e.New("Unknown logger")
+	UNKNOWN_LEVEL  = e.New("Unknown log level: TRACE|INFO|WARNING|ERROR|PANIC")
+
+	INVALID_PATH = e.New("Invalid path")
+)

+ 0 - 9
log/errors/errors.go

@@ -1,9 +0,0 @@
-package errors
-
-import (
-    e "errors"
-)
-
-var (
-    UNKNOWN_LOGGER = e.New("Unknown logger")
-)

+ 4 - 3
log/log.go

@@ -8,12 +8,12 @@ import (
 	"os"
 	"strings"
 
-	"source.uit.com/mikevoets/inf-3200-1/tools/log/config"
-	"source.uit.com/mikevoets/inf-3200-1/tools/log/errors"
+	"git.giaever.org/joachimmg/go-log.git/config"
+	"git.giaever.org/joachimmg/go-log.git/errors"
 )
 
 func log_file(p config.LogPrefix) *os.File {
-	os.MkdirAll(config.LOG_DIR, 0777)
+	os.MkdirAll(config.LOG_DIR.String(), 0777)
 	f, err := os.OpenFile(
 		fmt.Sprintf("%s%c%s%s.log", config.LOG_DIR, os.PathSeparator, strings.ToLower(string(p)), config.FILE_EXTRA),
 		config.FILE_MASK,
@@ -63,6 +63,7 @@ func log_new(l config.LogLevel, h config.LogHandle, p config.LogPrefix) *_log.Lo
 func init() {
 	flag.Var(&config.LOG_LEVEL_OUTPUT, "llo", "Minimum log level that will be written to stdout.")
 	flag.Var(&config.LOG_LEVEL_FILE, "llf", "Minimum log level that will be written to file")
+	flag.Var(&config.LOG_DIR, "ldir", "Directory to log to")
 }
 
 func log(t config.LogLevel) *_log.Logger {

+ 16 - 0
main.go

@@ -0,0 +1,16 @@
+package main
+
+import (
+	"flag"
+
+	"git.giaever.org/joachimmg/go-log.git/log"
+)
+
+func main() {
+	flag.Parse()
+	log.Tracef("This is a %s-log", "trace")
+	log.Infof("This is a %s-log", "info")
+	log.Warningf("This is a %s-log", "warning")
+	log.Errorf("This is a %s-log", "error")
+	log.Panicf("This is a %s-log", "panic")
+}