Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timestamp for debug logs #3110

Merged
merged 10 commits into from
Jan 10, 2023
Merged

Conversation

xm1k3
Copy link
Contributor

@xm1k3 xm1k3 commented Jan 1, 2023

Proposed changes

Added timestamp on --debug

echo example.com | ./nuclei -id azure-takeover-detection -debug -ts

[INF] [azure-takeover-detection] Dumped DNS request for example.com. domain=example.com.
;; opcode: QUERY, status: NOERROR, id: 52542
;; flags: rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version 0; flags: ; udp: 4096

;; QUESTION SECTION:
;example.com.	IN	 A
[DBG] [2023-01-10T21:02:03+05:30] [azure-takeover-detection] Dumped DNS response for example.com.

;; opcode: QUERY, status: NOERROR, id: 52542
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version 0; flags: ; udp: 512

;; QUESTION SECTION:
;example.com.	IN	 A

;; ANSWER SECTION:
example.com.	19274	IN	A	93.184.216.34
[INF] No results found. Better luck next time!

Changes Made

This PR includes the following changes:

  • Added the SetTimestamp method to the Logger struct in gologger, which allows the user to enable or disable automatic timestamps for log messages, and to set the minimum level of log messages for which timestamps should be included.
  • Updated the init() function to set the default maximum log level to LevelInfo, and added a timestamp option for the formatter and writer.

How to use

The package provides a DefaultLogger variable that can be used to log messages with different levels (Debug, Info, Error, etc.). You can configure the behavior of the DefaultLogger using various methods, such as SetTimestamp.

An example of how to use this package:

package main

import (
	"github.com/projectdiscovery/gologger"
)

func main() {
    gologger.DefaultLogger.SetTimestamp(true, levels.LevelDebug)
    gologger.Debug("This message will include a timestamp because its level is greater than or equal to LevelDebug")
}

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@xm1k3 xm1k3 self-assigned this Jan 1, 2023
@xm1k3 xm1k3 linked an issue Jan 1, 2023 that may be closed by this pull request
@xm1k3 xm1k3 added the Type: Enhancement Most issues will probably ask for additions or changes. label Jan 1, 2023
@xm1k3 xm1k3 requested a review from Mzack9999 January 2, 2023 22:54
Copy link
Member

@Mzack9999 Mzack9999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach doesn't seem very scalable. Each new added instruction will require the .Timestamp() call. I think we should either define an helper function that will invoke:

gologger.Debug().Timestamp().Msgf("xxx)

or make this configurable via gologger itself, for example with global setter like:

gologger.EnableTimestamp()
gologger.EnableTimestampForLevel(Debug|Info|etc)

Also, I think that the timestamp should be disabled by default, as it can make the output easily unreadable (similarly to #2928)

@xm1k3
Copy link
Contributor Author

xm1k3 commented Jan 3, 2023

Important note:
Before merging this PR, make sure that the PR (projectdiscovery/gologger#29) on the Logger has also been merged. The function called in this PR currently does not exist on the master branch of the Logger and would cause issues.

@xm1k3 xm1k3 requested a review from Mzack9999 January 3, 2023 13:43
Copy link
Member

@Mzack9999 Mzack9999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can test with the specific gologger commit with:

go get -v github.com/projectdiscovery/gologger@1c357ca

v2/cmd/nuclei/main.go Outdated Show resolved Hide resolved
@xm1k3 xm1k3 requested a review from Mzack9999 January 4, 2023 14:39
Copy link
Member

@Mzack9999 Mzack9999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might need to point gologger with the correct reference:

go get -v github.com/projectdiscovery/gologger@xxx

github.com/projectdiscovery/gologger@1c357ca
@xm1k3 xm1k3 requested a review from Mzack9999 January 5, 2023 10:39
Copy link
Member

@ehsandeep ehsandeep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • failing test
  • update PR with a working example.

@xm1k3 xm1k3 requested a review from ehsandeep January 10, 2023 15:20
@ehsandeep ehsandeep merged commit 63f1b9c into dev Jan 10, 2023
@ehsandeep ehsandeep deleted the issue-2363-add-timestamp-for-debug-logs branch January 10, 2023 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Most issues will probably ask for additions or changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add timestamps for the logs
3 participants