Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin committed Feb 20, 2020
0 parents commit 44d9c17
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Matthew Peveler

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# @cisl/zepto-logger

A minimalistic, zero-dependency logger that writes messages to the console with
the current timestamp. Good for debugging, but for a production grade
application, probably worth looking elsewhere.

## Installation

```bash
npm install @cisl/zepto-logger
```

## Usage

```javascript
const {setLogLevel, logExpression} = require('@cisl/zepto-logger');

// by default the logLevel starts at 1
logExpression('This message will print', 1);
logExpression('This message will not print', 2);

setLogLevel(2);
logExpression('This message will now print', 2);

// can also log objects and other types, not just strings
logExpression({foo: 1, bar: 2, baz: {a: 1, b: 2}}, 1);
```
2 changes: 2 additions & 0 deletions logger.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare function logLevel(level: number): void;
declare function logExpression(msg: any, level: number): void;
33 changes: 33 additions & 0 deletions logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
let logLevel = 1;

/**
* Set the log level.
*
* @param {number} level
*/
module.exports.setLogLevel = (level) => {
logLevel = level;
}

/**
* Log an expression to console at a specific level.
*
* @param {any} msg
* @param {number} level
*/
module.exports.logExpression = (msg, level) => {
if (level > logLevel) {
return;
}
const now = new Date();
const date = [now.getFullYear(), now.getMonth(), now.getDate()].map((val) => val.toString().padStart(2, '0')).join('-');
const time = [now.getHours(), now.getMinutes(), now.getSeconds()].map((val) => val.toString().padStart(2, '0')).join(':');
const datetime = `[${date} ${time}.${now.getMilliseconds().toString().padStart(3, '0').substr(0, 2)}]`;
if (typeof msg === 'object') {
console.log(datetime);
console.log(JSON.stringify(msg, null, 2));
}
else {
console.log(`${datetime} ${msg}`);
}
}
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@cisl/zepto-logger",
"version": "1.0.0",
"description": "A tiny logger utility library",
"main": "logger.js",
"types": "logger.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/cislrpi/zepto-logger.git"
},
"keywords": [
"logger"
],
"author": "Matthew Peveler <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/cislrpi/zepto-logger/issues"
},
"homepage": "https://github.com/cislrpi/zepto-logger#readme",
"dependencies": {}
}

0 comments on commit 44d9c17

Please sign in to comment.