-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
106 lines (88 loc) · 1.84 KB
/
logger.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// JSTP Logger
// ===========
//
// [ ![Codeship Status for jstp/logger](https://codeship.com/projects/897ccb40-de2d-0132-d439-465ff4e7e511/status?branch=master)](https://codeship.com/projects/80373)
//
// A nice logger for JSTP
//
// Installation
// ------------
//
// ```sh
// npm install --save @jstp/logger
// ```
//
// Usage
// -----
//
// ```javascript
// var Logger = require("@jstp/logger")
// var Sydney = require("sydney")
//
// var venue = new Sydney
// venue.add(new Logger)
//
// // And use it normally, it will log to the console
// ```
//
"use strict"
var Sydney = require("sydney")
var BOLD = "\u001b[1m"
var GREEN = "\u001b[32m"
var BLUE = "\u001b[34m"
var YELLOW = "\u001b[33m"
var MAGENTA = "\u001b[35m"
var RED = "\u001b[31m"
var GREY = "\u001b[30m"
var CLEAR = "\u001b[0m"
var Logger = function (theConsole) {
this.console = theConsole || console
}
Logger.prototype = Object.create(Sydney.prototype)
Logger.prototype.callback = function (event) {
var text = ""
var color = undefined
if (event.protocol instanceof Array)
text += GREY + event.protocol[0] + ":" + CLEAR
switch (event.method) {
case "GET":
color = BLUE
break
case "POST":
color = GREEN
break
case "PUT":
color = YELLOW
break
case "PATCH":
color = MAGENTA
break
case "DELETE":
color = RED
break
}
if (event.method)
text += color + event.method + CLEAR
if (event.resource instanceof Array)
text += GREY + "/" + CLEAR + event.resource.join(GREY + "/" + CLEAR)
this.console.log(text)
}
module.exports = Logger
// Testing
// -------
//
// ```
// git clone git://github.com/jstp/logger
// cd logger
// npm install
// npm test
// ```
//
// License
// -------
//
// Copyright 2015 Xavier Via
//
// ISC Clause license.
//
// See [LICENSE](LICENSE) attached.