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

Improve logger #487

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/analyzer/analyzer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def initialize_analyzers(logger : NoirLogger)
{"ai_ollama", AI::Ollama},
])

logger.success "#{analyzers.size} Analyzers initialized"
logger.debug "Analyzers:"
logger.debug "#{analyzers.size} Analyzers initialized"
analyzers.each do |key, _|
logger.debug_sub "#{key} initialized"
end
Expand All @@ -62,9 +61,6 @@ def analysis_endpoints(options : Hash(String, YAML::Any), techs, logger : NoirLo
logger.info "Initializing analyzers"

analyzer = initialize_analyzers logger
if options["url"] != ""
logger.sub "➔ File analyzer initialized and #{file_analyzer.hooks_count} hooks loaded"
end

logger.info "Analysis Started"
logger.sub "➔ Code Analyzer: #{techs.size} in use"
Expand All @@ -89,6 +85,6 @@ def analysis_endpoints(options : Hash(String, YAML::Any), techs, logger : NoirLo
result = result + file_analyzer.analyze
end

logger.sub "➔ Found #{result.size} endpoints"
logger.info "Found #{result.size} endpoints"
result
end
1 change: 1 addition & 0 deletions src/detector/detector.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def detect_techs(base_path : String, options : Hash(String, YAML::Any), passive_
detector_list.each do |detector|
if detector.detect(file, content)
techs << detector.name
logger.debug_sub "└── Detected: #{detector.name}"
end
end

Expand Down
90 changes: 46 additions & 44 deletions src/models/logger.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
require "colorize"

class NoirLogger
enum LogLevel
DEBUG
INFO
SUCCESS
WARNING
ERROR
FATAL
HEADING
end

def initialize(debug : Bool, colorize : Bool, no_log : Bool)
@debug = debug
@color_mode = colorize
@no_log = no_log
end

def log(level : LogLevel, message : String)
return if @no_log

prefix = case level
when LogLevel::DEBUG
"❏".colorize(:dark_gray).toggle(@color_mode)
when LogLevel::INFO
"⚑".colorize(:light_cyan).toggle(@color_mode)
when LogLevel::SUCCESS
"✔".colorize(:green).toggle(@color_mode)
when LogLevel::WARNING
"▲".colorize(:yellow).toggle(@color_mode)
when LogLevel::ERROR
"✖︎".colorize(:red).toggle(@color_mode)
when LogLevel::FATAL
"☠".colorize(:red).toggle(@color_mode)
when LogLevel::HEADING
"★".colorize(:yellow).toggle(@color_mode)
end

STDERR.puts "#{prefix} #{message}"

exit(1) if level == LogLevel::FATAL
end

def puts(message)
STDOUT.puts message
end
Expand All @@ -16,74 +51,41 @@ class NoirLogger
end

def heading(message)
if @no_log
return
end

prefix = "★".colorize(:yellow).toggle(@color_mode)
STDERR.puts "#{prefix} #{message}"
log(LogLevel::HEADING, message)
end

def info(message)
if @no_log
return
end

prefix = "⚑".colorize(:light_cyan).toggle(@color_mode)
STDERR.puts "#{prefix} #{message}"
log(LogLevel::INFO, message)
end

def success(message)
if @no_log
return
end

prefix = "✔".colorize(:green).toggle(@color_mode)
STDERR.puts "#{prefix} #{message}"
log(LogLevel::SUCCESS, message)
end

def sub(message)
if @no_log
return
end

return if @no_log
STDERR.puts " " + message
end

def warning(message)
prefix = "▲".colorize(:yellow).toggle(@color_mode)
STDERR.puts "#{prefix} #{message}"
log(LogLevel::WARNING, message)
end

def error(message)
prefix = "✖︎".colorize(:red).toggle(@color_mode)
STDERR.puts "#{prefix} #{message}"
log(LogLevel::ERROR, message)
end

def debug(message)
if @no_log
return
end

if @debug
prefix = "❏".colorize(:dark_gray).toggle(@color_mode)
STDERR.puts "#{prefix} #{message}"
end
return if @no_log || !@debug
log(LogLevel::DEBUG, message.to_s)
end

def debug_sub(message)
if @no_log
return
end

if @debug
STDERR.puts " " + message.to_s
end
return if @no_log || !@debug
STDERR.puts " " + message.to_s
end

def fatal(message)
prefix = "☠".colorize(:red).toggle(@color_mode)
STDERR.puts "#{prefix} #{message}"
exit(1)
log(LogLevel::FATAL, message)
end
end
11 changes: 8 additions & 3 deletions src/models/noir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class NoirRunner

def optimize_endpoints
@logger.info "Optimizing endpoints."
@logger.sub "➔ Removing duplicated endpoints and params."
final = [] of Endpoint

@endpoints.each do |endpoint|
Expand Down Expand Up @@ -164,6 +165,7 @@ class NoirRunner
is_new = true
final.each do |dup|
if dup.method == tiny_tmp.method && dup.url == tiny_tmp.url
@logger.debug_sub " + Found duplicated endpoint: #{tiny_tmp.method} #{tiny_tmp.url}"
is_new = false
tiny_tmp.params.each do |param|
existing_param = dup.params.find { |dup_param| dup_param.name == param.name }
Expand Down Expand Up @@ -235,7 +237,9 @@ class NoirRunner
target_url = @options["url"].to_s

if target_url != ""
@logger.info "Combining url and endpoints."
@logger.sub "➔ Combining url and endpoints."
@logger.debug_sub " + Before size: #{@endpoints.size}"

@endpoints.each do |endpoint|
tmp_endpoint = endpoint
if tmp_endpoint.url.includes? target_url
Expand All @@ -255,12 +259,13 @@ class NoirRunner
tmp << tmp_endpoint
end

@logger.debug_sub " + After size: #{tmp.size}"
@endpoints = tmp
end
end

def add_path_parameters
@logger.info "Adding path parameters by URL."
@logger.sub "➔ Adding path parameters by URL."
final = [] of Endpoint

@endpoints.each do |endpoint|
Expand Down Expand Up @@ -304,7 +309,7 @@ class NoirRunner
end

def update_status_codes
@logger.info "Updating status codes."
@logger.sub "➔ Updating status codes."
final = [] of Endpoint

exclude_codes = [] of Int32
Expand Down
2 changes: 1 addition & 1 deletion src/noir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ app.logger.success "Finally identified #{app.endpoints.size} endpoints."
end_time = Time.monotonic
elapsed_time = end_time - start_time

app.logger.info "Scan completed in #{elapsed_time.total_milliseconds.round} ms."
app.logger.info "Scan completed in #{(elapsed_time.total_milliseconds / 1000.0).round(4)} s."

if app_diff.nil?
app.logger.info "Generating Report."
Expand Down
Loading