11const colors = require ( 'colors/safe' ) ;
2+ const merge = require ( 'deepmerge' ) ;
3+ const { inspect } = require ( 'util' ) ;
4+
5+ const { caller } = require ( './node' ) ;
26
37const LEVELS = Object . freeze ( {
4- NONE : 0 ,
5- ERROR : 1 ,
6- WARN : 2 ,
7- INFO : 3 ,
8- DEBUG : 4
8+ none : 0 ,
9+ error : 1 ,
10+ warn : 2 ,
11+ info : 3 ,
12+ debug : 4
913} ) ;
1014
1115const COLORS = Object . freeze ( {
@@ -16,24 +20,98 @@ const COLORS = Object.freeze({
1620} ) ;
1721
1822const FORMAT = Object . freeze ( {
19- ascii : 'ascii ' ,
23+ term : 'term ' ,
2024 text : 'text' ,
2125 json : 'json'
2226} ) ;
2327
28+ const TIMEOF = Object . freeze ( {
29+ iso : 'iso' ,
30+ ru : 'ru' ,
31+ en : 'en'
32+ } ) ;
33+
2434function getDefaultOptions ( ) {
2535 return {
26- level : LEVELS . INFO ,
36+ level : LEVELS . info ,
37+ time : TIMEOF . iso ,
2738 width : {
28- category : 40
39+ object : 40
2940 }
3041 } ;
3142}
3243
44+ let options = getDefaultOptions ( ) ;
45+
46+ function setOptions ( opts = { } ) {
47+ options = getOptions ( opts ) ;
48+ }
3349
34- module . exports = {
35- LEVELS ,
36- COLORS ,
37- FORMAT
50+ function getOptions ( opts = { } ) {
51+ return merge . all ( [ getDefaultOptions ( ) , options || { } , opts ] ) ;
52+ }
3853
54+ function summary ( message ) {
55+ if ( ! message ) {
56+ return '[undefined]' ;
57+ }
58+ if ( message instanceof Error ) {
59+ const joiner = colors . red ( '* ' ) ;
60+ return message . stack . split ( ' at' ) . join ( joiner ) ;
61+ }
62+ return inspect ( message ) ;
3963}
64+
65+ function log ( message = '?' , object = caller ( ) , level = LEVELS . info ) {
66+ const opts = getOptions ( ) ;
67+
68+ if ( level > opts . level || level === LEVELS . NONE ) {
69+ return ;
70+ }
71+ if ( message && message . _silent ) {
72+ return ;
73+ }
74+ const color = COLORS [ level ] ;
75+ let date = new Date ( ) . toISOString ( ) ; //utils.formatDate(new Date());
76+
77+ let line = '' ;
78+
79+ if ( typeof object === 'object' && object . module ) {
80+ object = object . module ;
81+ }
82+
83+ if ( object ) {
84+ if ( ! isNaN ( + object ) ) {
85+ object = `[${ object } ]` ;
86+ }
87+ object = ( object || '' ) . toString ( ) . substring ( 0 , opts . width . object ) ;
88+ object = `${ object . padEnd ( opts . width . object , ' ' ) } : ` ;
89+ line += colors . bold ( object ) ;
90+ }
91+
92+ if ( typeof message === 'object' ) {
93+ message = summary ( message ) ;
94+ } else {
95+ message = message + '' ;
96+ }
97+ if ( color ) {
98+ date = colors [ color ] ( date ) ;
99+ line = colors [ color ] ( line ) ;
100+ }
101+ console . log ( `${ date } ${ line } ${ message } ` ) ;
102+ }
103+
104+ log . LEVELS = LEVELS ;
105+ log . COLORS = COLORS ;
106+ log . FORMAT = FORMAT ;
107+
108+ log . setOptions = setOptions ;
109+ log . getOptions = getOptions ;
110+
111+ log . debug = ( message , object ) => log ( message , object || caller ( ) , LEVELS . debug ) ;
112+ log . info = ( message , object ) => log ( message , object || caller ( ) , LEVELS . info ) ;
113+ log . warn = ( message , object ) => log ( message , object || caller ( ) , LEVELS . warn ) ;
114+ log . error = ( message , object ) => log ( message , object || caller ( ) , LEVELS . error ) ;
115+
116+
117+ module . exports = log ;
0 commit comments