Skip to content

Commit 7670b2c

Browse files
authored
Merge pull request #487 from owasp-noir/issue-480
Improve logger
2 parents 52a6753 + 0f86d78 commit 7670b2c

File tree

5 files changed

+58
-54
lines changed

5 files changed

+58
-54
lines changed

src/analyzer/analyzer.cr

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ def initialize_analyzers(logger : NoirLogger)
4848
{"ai_ollama", AI::Ollama},
4949
])
5050

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

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

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

92-
logger.sub "Found #{result.size} endpoints"
88+
logger.info "Found #{result.size} endpoints"
9389
result
9490
end

src/detector/detector.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def detect_techs(base_path : String, options : Hash(String, YAML::Any), passive_
7777
detector_list.each do |detector|
7878
if detector.detect(file, content)
7979
techs << detector.name
80+
logger.debug_sub "└── Detected: #{detector.name}"
8081
end
8182
end
8283

src/models/logger.cr

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
11
require "colorize"
22

33
class NoirLogger
4+
enum LogLevel
5+
DEBUG
6+
INFO
7+
SUCCESS
8+
WARNING
9+
ERROR
10+
FATAL
11+
HEADING
12+
end
13+
414
def initialize(debug : Bool, colorize : Bool, no_log : Bool)
515
@debug = debug
616
@color_mode = colorize
717
@no_log = no_log
818
end
919

20+
def log(level : LogLevel, message : String)
21+
return if @no_log
22+
23+
prefix = case level
24+
when LogLevel::DEBUG
25+
"".colorize(:dark_gray).toggle(@color_mode)
26+
when LogLevel::INFO
27+
"".colorize(:light_cyan).toggle(@color_mode)
28+
when LogLevel::SUCCESS
29+
"".colorize(:green).toggle(@color_mode)
30+
when LogLevel::WARNING
31+
"".colorize(:yellow).toggle(@color_mode)
32+
when LogLevel::ERROR
33+
"✖︎".colorize(:red).toggle(@color_mode)
34+
when LogLevel::FATAL
35+
"".colorize(:red).toggle(@color_mode)
36+
when LogLevel::HEADING
37+
"".colorize(:yellow).toggle(@color_mode)
38+
end
39+
40+
STDERR.puts "#{prefix} #{message}"
41+
42+
exit(1) if level == LogLevel::FATAL
43+
end
44+
1045
def puts(message)
1146
STDOUT.puts message
1247
end
@@ -16,74 +51,41 @@ class NoirLogger
1651
end
1752

1853
def heading(message)
19-
if @no_log
20-
return
21-
end
22-
23-
prefix = "".colorize(:yellow).toggle(@color_mode)
24-
STDERR.puts "#{prefix} #{message}"
54+
log(LogLevel::HEADING, message)
2555
end
2656

2757
def info(message)
28-
if @no_log
29-
return
30-
end
31-
32-
prefix = "".colorize(:light_cyan).toggle(@color_mode)
33-
STDERR.puts "#{prefix} #{message}"
58+
log(LogLevel::INFO, message)
3459
end
3560

3661
def success(message)
37-
if @no_log
38-
return
39-
end
40-
41-
prefix = "".colorize(:green).toggle(@color_mode)
42-
STDERR.puts "#{prefix} #{message}"
62+
log(LogLevel::SUCCESS, message)
4363
end
4464

4565
def sub(message)
46-
if @no_log
47-
return
48-
end
49-
66+
return if @no_log
5067
STDERR.puts " " + message
5168
end
5269

5370
def warning(message)
54-
prefix = "".colorize(:yellow).toggle(@color_mode)
55-
STDERR.puts "#{prefix} #{message}"
71+
log(LogLevel::WARNING, message)
5672
end
5773

5874
def error(message)
59-
prefix = "✖︎".colorize(:red).toggle(@color_mode)
60-
STDERR.puts "#{prefix} #{message}"
75+
log(LogLevel::ERROR, message)
6176
end
6277

6378
def debug(message)
64-
if @no_log
65-
return
66-
end
67-
68-
if @debug
69-
prefix = "".colorize(:dark_gray).toggle(@color_mode)
70-
STDERR.puts "#{prefix} #{message}"
71-
end
79+
return if @no_log || !@debug
80+
log(LogLevel::DEBUG, message.to_s)
7281
end
7382

7483
def debug_sub(message)
75-
if @no_log
76-
return
77-
end
78-
79-
if @debug
80-
STDERR.puts " " + message.to_s
81-
end
84+
return if @no_log || !@debug
85+
STDERR.puts " " + message.to_s
8286
end
8387

8488
def fatal(message)
85-
prefix = "".colorize(:red).toggle(@color_mode)
86-
STDERR.puts "#{prefix} #{message}"
87-
exit(1)
89+
log(LogLevel::FATAL, message)
8890
end
8991
end

src/models/noir.cr

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class NoirRunner
135135

136136
def optimize_endpoints
137137
@logger.info "Optimizing endpoints."
138+
@logger.sub "➔ Removing duplicated endpoints and params."
138139
final = [] of Endpoint
139140

140141
@endpoints.each do |endpoint|
@@ -164,6 +165,7 @@ class NoirRunner
164165
is_new = true
165166
final.each do |dup|
166167
if dup.method == tiny_tmp.method && dup.url == tiny_tmp.url
168+
@logger.debug_sub " + Found duplicated endpoint: #{tiny_tmp.method} #{tiny_tmp.url}"
167169
is_new = false
168170
tiny_tmp.params.each do |param|
169171
existing_param = dup.params.find { |dup_param| dup_param.name == param.name }
@@ -235,7 +237,9 @@ class NoirRunner
235237
target_url = @options["url"].to_s
236238

237239
if target_url != ""
238-
@logger.info "Combining url and endpoints."
240+
@logger.sub "➔ Combining url and endpoints."
241+
@logger.debug_sub " + Before size: #{@endpoints.size}"
242+
239243
@endpoints.each do |endpoint|
240244
tmp_endpoint = endpoint
241245
if tmp_endpoint.url.includes? target_url
@@ -255,12 +259,13 @@ class NoirRunner
255259
tmp << tmp_endpoint
256260
end
257261

262+
@logger.debug_sub " + After size: #{tmp.size}"
258263
@endpoints = tmp
259264
end
260265
end
261266

262267
def add_path_parameters
263-
@logger.info "Adding path parameters by URL."
268+
@logger.sub "Adding path parameters by URL."
264269
final = [] of Endpoint
265270

266271
@endpoints.each do |endpoint|
@@ -304,7 +309,7 @@ class NoirRunner
304309
end
305310

306311
def update_status_codes
307-
@logger.info "Updating status codes."
312+
@logger.sub "Updating status codes."
308313
final = [] of Endpoint
309314

310315
exclude_codes = [] of Int32

src/noir.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ app.logger.success "Finally identified #{app.endpoints.size} endpoints."
124124
end_time = Time.monotonic
125125
elapsed_time = end_time - start_time
126126

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

129129
if app_diff.nil?
130130
app.logger.info "Generating Report."

0 commit comments

Comments
 (0)