Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "logplease", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Simple Javascript logger for Node.js and Browsers", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "node test/test.js" | ||
"example": "node example/example.js", | ||
"example:browser": "webpack --config webpack.example.config.js", | ||
"dist": "webpack --config webpack.config.js" | ||
}, | ||
@@ -9,0 +11,0 @@ "repository": { |
@@ -17,6 +17,18 @@ # logplease | ||
### Examples | ||
#### Node.js | ||
``` | ||
npm run example | ||
``` | ||
#### Browser | ||
*NOTE: Requires webpack to be installed globally! `npm install webpack -g`* | ||
``` | ||
npm run example:browser | ||
``` | ||
And open `example/index.html` in your browser. | ||
## Usage | ||
You can run `npm test` for an example output, see https://github.com/haadcode/logplease/test/test.js | ||
### Example | ||
```javascript | ||
@@ -28,3 +40,3 @@ const Logger = require('logplease'); | ||
const logger2 = Logger.create('utils', { color: Logger.Colors.Yellow }); | ||
const logger3 = Logger.create('logger3', { color: Logger.Colors.Magenta, showTimestamp: false, showLevel: false, output: process.stderr }); | ||
const logger3 = Logger.create('logger3', { color: Logger.Colors.Magenta, showTimestamp: false, showLevel: false }); | ||
@@ -79,3 +91,3 @@ // Set global log level | ||
``` | ||
Black, Red, Green, Yellow, Blue, Magenta, Cyan, White | ||
Black, Red, Green, Yellow, Blue, Magenta, Cyan, Grey, White | ||
``` | ||
@@ -93,11 +105,20 @@ | ||
```javascript | ||
const defaultOptions = { | ||
useColors: true, | ||
color: Colors.White, | ||
showTimestamp: true, | ||
showLevel: true, | ||
filename: null, | ||
appendFile: true, | ||
output: process.stdout | ||
const options = { | ||
useColors: true, // Enable colors | ||
color: Colors.White, // Set the color of the logger | ||
showTimestamp: true, // Display timestamp in the log message | ||
showLevel: true, // Display log level in the log message | ||
filename: null, // Set file path to log to a file | ||
appendFile: true, // Append logfile instead of overwriting | ||
}; | ||
``` | ||
### Build | ||
*NOTE: Requires webpack to be installed globally! `npm install webpack -g`* | ||
The build command will build the browser distributable. Note that for Node.js it is not needed to run the build command. | ||
``` | ||
npm run dist | ||
``` | ||
The distributable file will be located in `dist/index.js` |
'use strict'; | ||
const fs = require('fs'); | ||
const stream = require('stream'); | ||
const fs = require('fs'); | ||
const isNodejs = process.env.NODE ? true : false; | ||
const LogLevels = { | ||
@@ -14,3 +15,3 @@ 'DEBUG': 'DEBUG', | ||
const Colors = { | ||
let Colors = { | ||
'Black': 0, | ||
@@ -23,6 +24,22 @@ 'Red': 1, | ||
'Cyan': 6, | ||
'White': 7, | ||
'Grey': 7, | ||
'White': 9, | ||
'Default': 9, | ||
}; | ||
if(!isNodejs) { | ||
Colors = { | ||
'Black': 'Black', | ||
'Red': 'IndianRed', | ||
'Green': 'LimeGreen', | ||
'Yellow': 'Orange', | ||
'Blue': 'RoyalBlue', | ||
'Magenta': 'Orchid', | ||
'Cyan': 'SkyBlue', | ||
'Grey': 'DimGrey', | ||
'White': 'White', | ||
'Default': 'Black', | ||
}; | ||
} | ||
const defaultOptions = { | ||
@@ -35,6 +52,5 @@ useColors: true, | ||
appendFile: true, | ||
output: process.stdout | ||
}; | ||
const loglevelColors = [Colors.Cyan, Colors.Green, Colors.Yellow, Colors.Red, Colors.Black]; | ||
const loglevelColors = [Colors.Cyan, Colors.Green, Colors.Yellow, Colors.Red, Colors.Default]; | ||
let GlobalLogLevel = LogLevels.DEBUG; | ||
@@ -55,4 +71,2 @@ | ||
} | ||
this.out = this.options.output; | ||
} | ||
@@ -85,6 +99,17 @@ | ||
if(this.fileWriter) | ||
this.fileWriter.write(unformattedText); | ||
this.fileWriter.write(unformattedText + '\n'); | ||
if(this.out) | ||
this.out.write(formattedText) | ||
if(isNodejs) { | ||
console.log(formattedText) | ||
} else { | ||
if(this.options.showTimestamp && this.options.showLevel) { | ||
console.log(formattedText, format.timestamp, format.level, format.category, format.text) | ||
} else if(this.options.showTimestamp && !this.options.showLevel) { | ||
console.log(formattedText, format.timestamp, format.category, format.text) | ||
} else if(!this.options.showTimestamp && this.options.showLevel) { | ||
console.log(formattedText, format.level, format.category, format.text) | ||
} else { | ||
console.log(formattedText, format.category, format.text) | ||
} | ||
} | ||
} | ||
@@ -99,9 +124,22 @@ | ||
if(this.options.useColors) { | ||
const levelColor = Object.keys(LogLevels).map((f) => LogLevels[f]).indexOf(level); | ||
const categoryColor = this.options.color; | ||
if(isNodejs) { | ||
const levelColor = Object.keys(LogLevels).map((f) => LogLevels[f]).indexOf(level); | ||
const categoryColor = this.options.color; | ||
timestampFormat = '\u001b[3' + loglevelColors[levelColor] + 'm'; | ||
levelFormat = '\u001b[3' + loglevelColors[levelColor] + ';22m'; | ||
categoryFormat = '\u001b[3' + categoryColor + ';1m'; | ||
textFormat = '\u001b[0m: '; | ||
if(this.options.showTimestamp) | ||
timestampFormat = '\u001b[3' + Colors.Grey + 'm'; | ||
// timestampFormat = '\u001b[3' + loglevelColors[levelColor] + 'm'; | ||
levelFormat = '\u001b[3' + loglevelColors[levelColor] + ';22m'; | ||
categoryFormat = '\u001b[3' + categoryColor + ';1m'; | ||
textFormat = '\u001b[0m: '; | ||
} else { | ||
const levelColor = Object.keys(LogLevels).map((f) => LogLevels[f]).indexOf(level); | ||
const categoryColor = this.options.color; | ||
if(this.options.showTimestamp) | ||
timestampFormat = 'color:' + Colors.Grey; | ||
levelFormat = 'color:' + loglevelColors[levelColor]; | ||
categoryFormat = 'color:' + categoryColor + '; font-weight: bold'; | ||
} | ||
} | ||
@@ -123,6 +161,14 @@ | ||
if(!isNodejs) { | ||
if(this.options.showTimestamp) | ||
timestampFormat = '%c'; | ||
levelFormat = '%c'; | ||
categoryFormat = '%c'; | ||
textFormat = ': %c'; | ||
} | ||
let final = ''; | ||
if(this.options.showTimestamp) | ||
final += '[' + new Date().toISOString() + '] '; | ||
final += '' + new Date().toISOString() + ' '; | ||
@@ -136,3 +182,3 @@ final = timestampFormat + final; | ||
final += textFormat + text; | ||
final = final + '\n'; | ||
final = final + ''; | ||
return final; | ||
@@ -139,0 +185,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
311724
13
1104
121
2
1