Merge logawl package into master #10
4 changed files with 47 additions and 25 deletions
1
cli.go
1
cli.go
|
@ -136,6 +136,7 @@ func prepareCLI() *cli.App {
|
|||
&cli.BoolFlag{
|
||||
Name: "debug",
|
||||
Usage: "enable debug logging",
|
||||
Value: false,
|
||||
},
|
||||
},
|
||||
Action: doQuery,
|
||||
|
|
|
@ -33,8 +33,7 @@ func (l *Logger) level() Level {
|
|||
}
|
||||
|
||||
// Unmarshalls the int value of level for writing the header
|
||||
func (l *Logger) UnMarshalLevel() (string, error) {
|
||||
lv := l.GetLevel()
|
||||
func (l *Logger) UnMarshalLevel(lv Level) (string, error) {
|
||||
switch lv {
|
||||
case 0:
|
||||
return "fatal: ", nil
|
||||
|
@ -48,6 +47,10 @@ func (l *Logger) UnMarshalLevel() (string, error) {
|
|||
return "", fmt.Errorf("Invalid log level choice")
|
||||
}
|
||||
|
||||
func (l *Logger) IsLevel(level Level) bool {
|
||||
return l.level() >= level
|
||||
}
|
||||
|
||||
var AllLevels = []Level{
|
||||
FatalLevel,
|
||||
ErrorLevel,
|
||||
|
|
|
@ -12,34 +12,37 @@ import (
|
|||
// Level can be changed to one of the other log levels (FatalLevel, ErrorLevel, InfoLevel, DebugLevel)
|
||||
func New() *Logger {
|
||||
return &Logger{
|
||||
Out: os.Stderr,
|
||||
//TODO: Make cli.go change this value when calling logawl.New()
|
||||
Out: os.Stderr,
|
||||
Level: InfoLevel, //Default value is InfoLevel
|
||||
}
|
||||
}
|
||||
|
||||
// Takes any and prints it out to Logger -> Out (io.Writer (default is std.Err))
|
||||
func (l *Logger) Println(v ...any) {
|
||||
func (l *Logger) Println(level Level, v ...any) {
|
||||
if atomic.LoadInt32(&l.isDiscard) != 0 {
|
||||
return
|
||||
}
|
||||
switch l.Level { //Goes through log levels and does stuff based on them (Fatal os.Exit...etc)
|
||||
case 0:
|
||||
l.Printer(0, fmt.Sprintln(v...)) //Fatal level
|
||||
os.Exit(1)
|
||||
case 1:
|
||||
l.Printer(1, fmt.Sprintln(v...)) //Error level
|
||||
case 2:
|
||||
l.Printer(2, fmt.Sprintln(v...)) //Info level
|
||||
case 3:
|
||||
l.Printer(3, fmt.Sprintln(v...)) //Debug level
|
||||
//If verbose is not set --debug etc print _nothing_
|
||||
if l.IsLevel(level) {
|
||||
switch level { //Goes through log levels and does stuff based on them (Fatal os.Exit...etc)
|
||||
case 0:
|
||||
l.Printer(0, fmt.Sprintln(v...)) //Fatal level
|
||||
os.Exit(1)
|
||||
case 1:
|
||||
l.Printer(1, fmt.Sprintln(v...)) //Error level
|
||||
case 2:
|
||||
l.Printer(2, fmt.Sprintln(v...)) //Info level
|
||||
case 3:
|
||||
l.Printer(3, fmt.Sprintln(v...)) //Debug level
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Formats the log header as such <LogLevel> YYYY/MM/DD HH:MM:SS (local time) <the message to log>
|
||||
func (l *Logger) formatHeader(buf *[]byte, t time.Time, line int) {
|
||||
if lvl, err := l.UnMarshalLevel(); err == nil {
|
||||
func (l *Logger) formatHeader(buf *[]byte, t time.Time, line int, level Level) {
|
||||
if lvl, err := l.UnMarshalLevel(level); err == nil {
|
||||
*buf = append(*buf, lvl...)
|
||||
year, month, day := t.Date()
|
||||
formatter(buf, year, 4)
|
||||
|
@ -63,14 +66,14 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, line int) {
|
|||
}
|
||||
|
||||
// Printer prints the formatted message directly to stdErr
|
||||
func (l *Logger) Printer(level int, s string) error {
|
||||
func (l *Logger) Printer(level Level, s string) error {
|
||||
now := time.Now()
|
||||
var line int
|
||||
l.Mu.Lock()
|
||||
defer l.Mu.Unlock()
|
||||
|
||||
l.buf = l.buf[:0]
|
||||
l.formatHeader(&l.buf, now, line)
|
||||
l.formatHeader(&l.buf, now, line, level)
|
||||
l.buf = append(l.buf, s...)
|
||||
if len(s) == 0 || s[len(s)-1] != '\n' {
|
||||
l.buf = append(l.buf, '\n')
|
||||
|
@ -99,3 +102,18 @@ func formatter(buf *[]byte, i int, wid int) {
|
|||
b[bp] = byte('0' + i)
|
||||
*buf = append(*buf, b[bp:]...)
|
||||
}
|
||||
|
||||
// Call print directly with Debug level
|
||||
func (l *Logger) Debug(v ...any) {
|
||||
l.Println(DebugLevel, v...)
|
||||
}
|
||||
|
||||
// Call print directly with Info level
|
||||
func (l *Logger) Info(v ...any) {
|
||||
l.Println(InfoLevel, v...)
|
||||
}
|
||||
|
||||
// Call print directly with Error level
|
||||
func (l *Logger) Error(v ...any) {
|
||||
l.Println(ErrorLevel, v...)
|
||||
}
|
||||
|
|
10
query.go
10
query.go
|
@ -20,20 +20,20 @@ func doQuery(c *cli.Context) error {
|
|||
err error
|
||||
resp util.Response
|
||||
isHTTPS bool
|
||||
Logger = logawl.New() //init logger
|
||||
)
|
||||
|
||||
resp.Answers, err = parseArgs(c.Args().Slice())
|
||||
if err != nil {
|
||||
Logger.Error("Unable to parse args")
|
||||
return err
|
||||
}
|
||||
|
||||
logger := logawl.New() //init logger
|
||||
port := c.Int("port")
|
||||
if c.Bool("debug") {
|
||||
logger.SetLevel(3) //set level to debug
|
||||
Logger.SetLevel(3)
|
||||
}
|
||||
|
||||
port := c.Int("port")
|
||||
|
||||
Logger.Debug("Starting awl")
|
||||
// If port is not set, set it
|
||||
if port == 0 {
|
||||
if c.Bool("tls") || c.Bool("quic") {
|
||||
|
|
Loading…
Reference in a new issue