Merge logawl package into master #10

Merged
sam merged 13 commits from grumbulon/awl:master into master 2022-06-29 22:57:04 +00:00
5 changed files with 48 additions and 13 deletions
Showing only changes of commit f7e5a7adee - Show all commits

5
cli.go
View file

@ -138,6 +138,11 @@ func prepareCLI() *cli.App {
Usage: "enable debug logging",
Value: false,
},
&cli.BoolFlag{
Name: "test",
Usage: "enable debug logging",
Value: true,
},
},
Action: doQuery,
}

View file

@ -1,11 +1,30 @@
//Package for custom logging needs
package logawl
/*
LogAwl is the marriage of two logging libraries, the standard library logger and a hint of logorus, by grumbulon.
LogAwl is a package for custom logging needs
LogAwl extends the standard log library with support for log levels and some oop/not oop BS
LogAwl extends the standard log library with support for log levels
This is _different_ from the syslog package in the standard library because you do not define a file
Since AWL is a cli utility it writes directly to std err. In the future it may require support for writing to a log file or to syslog directly but for now
logawl will write to std err
because awl is a cli utility it writes directly to std err.
*/
// Use the New() function to init logawl
//
// logger := logawl.New()
//
// You can call specific logging levels from your new logger using
//
// logger.Debug("Message to log")
// logger.Fatal("Message to log")
// logger.Info("Message to log")
// logger.Error("Message to log")
//
// You may also set the log level on the fly with
//
// Logger.SetLevel(3)
// This allows you to change the default level (Info) and prevent log messages from being posted at higher verbosity levels
//for example if
// Logger.SetLevel(3)
// is not called and you call
// Logger.Debug()
// this runs through
// IsLevel(level)
// to verify if the debug log should be sent to std.Err or not based on the current expected log level
package logawl

View file

@ -36,13 +36,13 @@ func (l *Logger) level() Level {
func (l *Logger) UnMarshalLevel(lv Level) (string, error) {
switch lv {
case 0:
return "fatal: ", nil
return "FATAL ", nil
case 1:
return "error: ", nil
return "ERROR ", nil
case 2:
return "info: ", nil
return "INFO ", nil
case 3:
return "debug: ", nil
return "DEBUG ", nil
}
return "", fmt.Errorf("Invalid log level choice")
}

View file

@ -30,6 +30,7 @@ func (l *Logger) Println(level Level, v ...any) {
os.Exit(1)
case 1:
l.Printer(1, fmt.Sprintln(v...)) //Error level
os.Exit(2)
case 2:
l.Printer(2, fmt.Sprintln(v...)) //Info level
case 3:
@ -43,8 +44,11 @@ func (l *Logger) Println(level Level, v ...any) {
// 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, level Level) {
if lvl, err := l.UnMarshalLevel(level); err == nil {
// This is ugly but functional
// maybe there can be an append func or something in the future
*buf = append(*buf, lvl...)
year, month, day := t.Date()
*buf = append(*buf, '[')
formatter(buf, year, 4)
*buf = append(*buf, '/')
formatter(buf, int(month), 2)
@ -57,6 +61,8 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, line int, level Level) {
formatter(buf, min, 2)
*buf = append(*buf, ':')
formatter(buf, sec, 2)
*buf = append(*buf, ']')
*buf = append(*buf, ':')
*buf = append(*buf, ' ')
} else {
fmt.Printf("Unable to unmarshal log level: %v", err)
@ -117,3 +123,8 @@ func (l *Logger) Info(v ...any) {
func (l *Logger) Error(v ...any) {
l.Println(ErrorLevel, v...)
}
// Call print directly with Fatal level
func (l *Logger) Fatal(v ...any) {
l.Println(FatalLevel, v...)
}

View file

@ -73,8 +73,6 @@ func doQuery(c *cli.Context) error {
msg.SetQuestion(resp.Answers.Name, resp.Answers.Request)
// TODO: maybe not make this a gross chunk of if statements? who knows
// Make this authoritative (does this do anything?)
if c.Bool("aa") {
msg.Authoritative = true
@ -85,6 +83,7 @@ func doQuery(c *cli.Context) error {
}
// Set the zero flag if requested (does nothing)
if c.Bool("z") {
Logger.Debug("Setting message to zero")
msg.Zero = true
}
// Disable DNSSEC validation
@ -101,6 +100,7 @@ func doQuery(c *cli.Context) error {
}
// Set DNSSEC if requested
if c.Bool("dnssec") {
Logger.Debug("Using DNSSEC")
msg.SetEdns0(1232, true)
}