| 
									
										
										
										
											2021-05-20 08:55:18 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { format } = require('winston'); | 
					
						
							|  |  |  | const { isString } = require('lodash/fp'); | 
					
						
							| 
									
										
										
										
											2021-06-29 16:27:35 +02:00
										 |  |  | const logErrors = require('./log-errors'); | 
					
						
							| 
									
										
										
										
											2021-05-20 08:55:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | const { combine, timestamp, colorize, printf } = format; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const defaultTimestampFormat = 'YYYY-MM-DD HH:mm:ss.SSS'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Create a pretty print formatter for a winston logger | 
					
						
							|  |  |  |  * @param {string|boolean} timestamps - Enable or disable timestamps print if it's a boolean value. Use the given format for the timestamps if it's a string | 
					
						
							|  |  |  |  * @param {boolean} colors - Enable or disable the use of colors for the log level | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | module.exports = ({ timestamps = true, colors = true } = {}) => { | 
					
						
							|  |  |  |   const handlers = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (timestamps) { | 
					
						
							|  |  |  |     handlers.push( | 
					
						
							|  |  |  |       timestamp({ | 
					
						
							|  |  |  |         format: isString(timestamps) ? timestamps : defaultTimestampFormat, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (colors) { | 
					
						
							|  |  |  |     handlers.push(colorize()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 16:27:35 +02:00
										 |  |  |   handlers.push(logErrors()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-20 08:55:18 +02:00
										 |  |  |   handlers.push( | 
					
						
							|  |  |  |     printf(({ level, message, timestamp }) => { | 
					
						
							|  |  |  |       return `${timestamps ? `[${timestamp}] ` : ''}${level}: ${message}`; | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return combine(...handlers); | 
					
						
							|  |  |  | }; |