-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag-formatter.coffee
43 lines (33 loc) · 1.04 KB
/
tag-formatter.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{Formatter} = require 'caterpillar'
# Array white list
displayedTags = []
class TagFormatter extends Formatter
# getTime
getDate: ->
# Prepare
now = new Date()
hours = @padLeft '0', 2, now.getHours()
minutes = @padLeft '0', 2, now.getMinutes()
seconds = @padLeft '0', 2, now.getSeconds()
ms = @padLeft '0', 3, now.getMilliseconds()
# Return
"#{hours}:#{minutes}:#{seconds}.#{ms}"
# [tag1,tag2, tag3] message
getTags: (message) ->
# Get the content between the first pair of square brackets
pattern = /^\[([\w, ]+)\]/i
tags = pattern.exec message
return [] unless tags
tags = tags[1].split /[ ,]+/
format: (levelCode,levelName,args) ->
# Prepare
{date,file,line,method,color,levelName,message} = @details(levelCode, levelName, args)
# Check
return message if !message
tags = @getTags message
# Display everything by default if white list empty
if (displayedTags.length is 0) or (true in (val in displayedTags for val in tags))
"[#{date}] #{message}"
else
null
module.exports = TagFormatter