Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ptkdev/logger

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ptkdev/logger - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

examples/example-json.js

13

CHANGELOG.md

@@ -0,4 +1,11 @@

# v1.1.0 (March 08, 2020)
* Fix: misprint of `errors` in options and docs, renamed to `error`
* Fix: errors logs output moved to `stderr`
* Feature: new formats of logs files, available log (text) or (json)
* Compatibility: the JSON logs format is compatible with [pinojs](https://github.com/pinojs/pino) output
[![](https://img.shields.io/badge/donate-paypal-005EA6.svg?logo=paypal)](https://www.paypal.me/ptkdev) [![](https://img.shields.io/badge/donate-patreon-F87668.svg?logo=patreon)](https://www.patreon.com/ptkdev) [![](https://img.shields.io/badge/donate-sponsors-ea4aaa.svg?logo=github)](https://github.com/sponsors/ptkdev/) [![](https://img.shields.io/badge/donate-ko--fi-29abe0.svg?logo=ko-fi)](https://ko-fi.com/ptkdev)
# v1.0.0 (March 08, 2020)
* First Release.
[![](https://img.shields.io/badge/donate-paypal-005EA6.svg?logo=paypal)](https://www.paypal.me/ptkdev) [![](https://img.shields.io/badge/donate-patreon-F87668.svg?logo=patreon)](https://www.patreon.com/ptkdev) [![](https://img.shields.io/badge/donate-sponsors-ea4aaa.svg?logo=github)](https://github.com/sponsors/ptkdev/) [![](https://img.shields.io/badge/donate-ko--fi-29abe0.svg?logo=ko-fi)](https://ko-fi.com/ptkdev)
* First Release.

@@ -13,3 +13,3 @@ /**

const options = {
"language": "en", // set language of log type, NOTE: please help with translations! (optional, default en - values: en | it)
"language": "en", // set language of log type, NOTE: please help with translations! (optional, default en - values: en|it|pl)
"colors": true, // enable/disable colors in terminal (optional, default enabled - values: true|enabled or false|disabled)

@@ -19,5 +19,6 @@ "debug": true, // enable/disable all logs with method debug (optional, default enabled - values: true|enabled or false|disabled)

"warning": true, // enable/disable all logs with method warning (optional, default enabled - values: true|enabled or false|disabled)
"errors": true, // enable/disable all logs with method errors (optional, default enabled - values: true|enabled or false|disabled)
"error": true, // enable/disable all logs with method errors (optional, default enabled - values: true|enabled or false|disabled)
"sponsor": true, // enable/disable all logs with method sponsor (optional, default enabled - values: true|enabled or false|disabled)
"write": true, // write the logs into a file, you need set path values (optional, default disabled - values: true|enabled or false|disabled)
"type": "log", // format of logs in files (optional, default log - values: log|json)
"path": { // if write is true, the library writes the logs to a path

@@ -24,0 +25,0 @@ "debug_log": "./debug.log", // all logs

@@ -14,2 +14,4 @@ /**

const ansi = require("strip-ansi");
const lowdb = require("lowdb");
const FileSync = require("lowdb/adapters/FileSync");
const languages = {

@@ -53,4 +55,4 @@ "en": require("../translations/en"),

if (typeof options.errors === "undefined" || options.errors === null) {
options.errors = true;
if (typeof options.error === "undefined" || options.error === null) {
options.error = true;
}

@@ -77,10 +79,22 @@

* =====================
* Current (now) date and time for prefix of logs
* Current (now) date and time for prefix of logs. Timezone is supported.
*
* @param {string} format - format of date: json, timestamp or string (optional, deafult: string)
*
* @return {string} time - current Date.now()
*
*/
current_time() {
current_time(format = "string") {
let tz_offset = (new Date()).getTimezoneOffset() * 60000;
if (format === "json") {
return (new Date(Date.now() - tz_offset)).toISOString();
}
if (format === "timestamp") {
return (new Date(Date.now() - tz_offset)).getTime();
}
return (new Date(Date.now() - tz_offset)).toISOString().slice(0, -5).replace("T", " ");
}

@@ -99,15 +113,9 @@

if (this.config.write === "enabled" || this.config.write === true) {
if (tag !== "") {
tag = `${tag}: `;
}
let log_text = `[${this.current_time()}] [${type.id}] ${tag}${message}\n`;
fse.appendFile(this.config.path.debug_log, ansi(log_text), (err) => {
if (err) {
logger.log(err);
if (this.config.type === "log") {
if (tag !== "") {
tag = `${tag}: `;
}
});
let log_text = `[${this.current_time()}] [${type.id}] ${tag}${message}\n`;
if (type.id === "ERROR") {
fse.appendFile(this.config.path.error_log, ansi(log_text), (err) => {
fse.appendFile(this.config.path.debug_log, ansi(log_text), (err) => {
if (err) {

@@ -117,2 +125,43 @@ logger.log(err);

});
if (type.id === "ERROR") {
fse.appendFile(this.config.path.error_log, ansi(log_text), (err) => {
if (err) {
logger.err(err);
}
});
}
} else {
const debug_adapter = new FileSync(this.config.path.debug_log);
const debug_db = lowdb(debug_adapter);
const error_adapter = new FileSync(this.config.path.error_log);
const error_db = lowdb(error_adapter);
let level = 0;
switch (type.id) {
case "ERROR":
level = 1;
break;
case "WARNING":
level = 2;
break;
case "INFO":
level = 3;
break;
case "DEBUG":
level = 4;
break;
default:
level = 5;
break;
}
debug_db.defaults({logs: []}).write();
error_db.defaults({logs: []}).write();
debug_db.get("logs").push({level: level, time: this.current_time("timestamp"), date: this.current_time("json"), msg: ansi(message), tag: ansi(tag), v: 1}).write();
if (type.id === "ERROR") {
error_db.get("logs").push({level: level, time: this.current_time("timestamp"), date: this.current_time("json"), msg: ansi(message), tag: ansi(tag), v: 1}).write();
}
}

@@ -123,3 +172,3 @@ }

/**
* Output of console log
* Write to stdout
* =====================

@@ -146,2 +195,24 @@ * Log manager - don't use this directly. Use info() error() debug() warning()

/**
* Write to stderr
* =====================
* Log manager - don't use this directly. Use info() error() debug() warning()
*
* @param {string} type - example: INFO/WARNING/ERROR/DEBUG or other valid type string (see ./types.js) (mandatory)
* @param {string} tag - func unique tag (optional)
* @param {string} message - error, warning or info description (mandatory)
*
*/
err(type = "ERROR", tag = "", message = "") {
let time = this.TYPES_LOG.TIME;
if (tag !== "") {
tag = ` ${tag}:`;
}
if (this.config.colors === "enabled" || this.config.colors === true) {
logger.error(chalk`${type.bgcolor(type.label)}${time.bgcolor(` ${this.current_time()} `)}${type.bgcolor(" ")}${type.color(tag)} ${type.color(message)}`);
} else {
logger.error(ansi(chalk`${type.bgcolor(type.label)}${time.bgcolor(` ${this.current_time()} `)}${type.bgcolor(" ")}${type.color(tag)} ${type.color(message)}`));
}
}
/**
* Output of console log: type info

@@ -188,4 +259,4 @@ * =====================

error(message = "", tag = "") {
if (this.config.errors === "enabled" || this.config.errors === true) {
this.log(this.TYPES_LOG.ERROR, tag, `${message}`);
if (this.config.error === "enabled" || this.config.error === true) {
this.err(this.TYPES_LOG.ERROR, tag, `${message}`);
this.append_file(this.TYPES_LOG.ERROR, tag, message);

@@ -192,0 +263,0 @@ }

{
"name": "@ptkdev/logger",
"description": "Beautiful Node Logger: the best alternative to the console.log statement",
"version": "1.0.0",
"description": "Beautiful Logger for Node.js: the best alternative to the console.log statement",
"version": "1.1.0",
"main": "modules/logger.js",

@@ -54,2 +54,3 @@ "author": "Patryk Rzucidło [@ptkdev] <support@ptkdev.io> (https://ptk.dev)",

"chalk": "latest",
"lowdb": "latest",
"fs-extra": "latest",

@@ -56,0 +57,0 @@ "strip-ansi": "latest"

@@ -1,6 +0,6 @@

[![Beautiful Node Logger: the best alternative to the console.log statement](https://raw.githubusercontent.com/ptkdev/ptkdev-logger/nightly/.github/assets/ptkdev-logger-logo.png)](https://www.npmjs.com/package/@ptkdev/logger)
[![Beautiful Logger for Node.js: the best alternative to the console.log statement](https://raw.githubusercontent.com/ptkdev/ptkdev-logger/nightly/.github/assets/ptkdev-logger-logo.png)](https://www.npmjs.com/package/@ptkdev/logger)
# 🦒 Beautiful Node Logger
# 🦒 Beautiful Logger for Node.js
[![](https://img.shields.io/badge/version-v1.0.0-lightgrey.svg)](https://github.com/ptkdev/ptkdev-logger/releases) [![](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/ptkdev/ptkdev-logger/blob/master/LICENSE.md) [![](https://img.shields.io/badge/ES-9-F7DF1E.svg)](https://wikipedia.org/wiki/ECMAScript) [![](https://snyk.io/test/github/ptkdev/ptkdev-logger/badge.svg)](https://snyk.io/test/github/ptkdev/ptkdev-logger) [![](https://discordapp.com/api/guilds/383373985666301975/embed.png)](http://discord.ptkdev.io)
[![](https://img.shields.io/badge/version-v1.1.0-lightgrey.svg)](https://github.com/ptkdev/ptkdev-logger/releases) [![](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/ptkdev/ptkdev-logger/blob/master/LICENSE.md) [![](https://img.shields.io/badge/ES-9-F7DF1E.svg)](https://wikipedia.org/wiki/ECMAScript) [![](https://snyk.io/test/github/ptkdev/ptkdev-logger/badge.svg)](https://snyk.io/test/github/ptkdev/ptkdev-logger) [![](https://discordapp.com/api/guilds/383373985666301975/embed.png)](http://discord.ptkdev.io)

@@ -30,6 +30,8 @@ > The best alternative to the console.log statement

* [✔️] The best alternative to the console.log statement
* [✔️] Write stdout logs to file (supported format: text/log and json)
* [✔️] The JSON logs format is compatible with [pinojs](https://github.com/pinojs/pino)
* [✔️] Translations: 🇬🇧 🇮🇹 🇵🇱 (Help me ❤️)
## 👔 Screenshot
[![Beautiful Node Logger](https://raw.githubusercontent.com/ptkdev/ptkdev-logger/nightly/.github/assets/screenshot/ptkdev-logger-screen1.png)](https://raw.githubusercontent.com/ptkdev/ptkdev-logger/nightly/.github/assets/screenshot/ptkdev-logger-screen1.png)
[![Beautiful Logger for Node.js](https://raw.githubusercontent.com/ptkdev/ptkdev-logger/nightly/.github/assets/screenshot/ptkdev-logger-screen1.png)](https://raw.githubusercontent.com/ptkdev/ptkdev-logger/nightly/.github/assets/screenshot/ptkdev-logger-screen1.png)

@@ -55,5 +57,6 @@ ## 🚀 Installation

"warning": true,
"errors": true,
"error": true,
"sponsor": true,
"write": true,
"type": "log",
"path": {

@@ -80,5 +83,6 @@ "debug_log": "./debug.log",

| warning | Enable all logs with method warning | true\|enabled\|false\|disabled | true |
| errors | Enable all logs with method errors | true\|enabled\|false\|disabled | true |
| error | Enable all logs with method errors | true\|enabled\|false\|disabled | true |
| sponsor | Enable all logs with method sponsor | true\|enabled\|false\|disabled | true |
| write | Write the logs into a file, you need set path values | true\|enabled\|false\|disabled | false |
| write | Write the logs into a file, you need set path values | true\|enabled\|false\|disabled | false |
| type | Format of logs in files | log\|json | log |
| path | If write is true, the library writes the logs to a path | Object | `{"debug_log": "./debug.log", "error_log": "./errors.log"}` |

@@ -90,9 +94,9 @@

| --- | --- | --- |
| debug(__message__, __tag__) | `message`: Display debug log message <br> `tag`: prefix of message | `message`: string (mandatory) <br> `tag`: string (optional) |
| info(__message__, __tag__) | `message`: Display info log message <br> `tag`: prefix of message | `message`: string (mandatory) <br> `tag`: string (optional) |
| warning(__message__, __tag__) | `message`: Display warning log message <br> `tag`: prefix of message | `message`: string (mandatory) <br> `tag`: string (optional) |
| errors(__message__, __tag__) | `message`: Display errors log message <br> `tag`: prefix of message | `message`: string (mandatory) <br> `tag`: string (optional) |
| sponsor(__message__, __tag__) | `message`: Display sponsor log message <br> `tag`: prefix of message | `message`: string (mandatory) <br> `tag`: string (optional) |
| stackoverflow(__message__, __tag__, __error_string__) | `message`: Display stackoverflow log message <br> `tag`: prefix of message <br> `error_string`: query for stackoverflow, if empty we use message param | `message`: string (mandatory) <br> `tag`: string (optional) <br> `error_string`: string (optional) |
| docs(__message__, __url__, __tag__,) | `message`: Display docs log message <br> `url`: link of documentation <br> `tag`: prefix of message | `message`: string (mandatory) <br> `url`: string (optional) <br> `tag`: string (optional) |
| **debug**(__message__, __tag__) | `message`: Display debug log message <br> `tag`: prefix of message | `message`: string (mandatory) <br> `tag`: string (optional) |
| **info**(__message__, __tag__) | `message`: Display info log message <br> `tag`: prefix of message | `message`: string (mandatory) <br> `tag`: string (optional) |
| **warning**(__message__, __tag__) | `message`: Display warning log message <br> `tag`: prefix of message | `message`: string (mandatory) <br> `tag`: string (optional) |
| **error**(__message__, __tag__) | `message`: Display errors log message <br> `tag`: prefix of message | `message`: string (mandatory) <br> `tag`: string (optional) |
| **sponsor**(__message__, __tag__) | `message`: Display sponsor log message <br> `tag`: prefix of message | `message`: string (mandatory) <br> `tag`: string (optional) |
| **stackoverflow**(__message__, __tag__, __error_string__) | `message`: Display stackoverflow log message <br> `tag`: prefix of message <br> `error_string`: query for stackoverflow, if empty we use message param | `message`: string (mandatory) <br> `tag`: string (optional) <br> `error_string`: string (optional) |
| **docs**(__message__, __url__, __tag__) | `message`: Display docs log message <br> `url`: link of documentation <br> `tag`: prefix of message | `message`: string (mandatory) <br> `url`: string (optional) <br> `tag`: string (optional) |

@@ -99,0 +103,0 @@ ## 📚 Documentation

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc