├── docs └── img │ ├── file_screen1.png │ ├── file_screen2.png │ ├── console_screen1.png │ ├── console_screen2.png │ ├── console_screen3.png │ └── logmark_primary_color.png ├── src ├── utils │ ├── decorators.js │ └── colors.js └── core │ └── Logmark.js ├── CONTRIBUTING.md ├── SECURITY.md ├── example.js ├── pkg ├── index.d.ts └── index.js ├── .github └── ISSUE_TEMPLATE │ └── bug_report.md ├── LICENSE ├── package.json ├── .npmignore ├── .gitignore ├── README.md └── CODE_OF_CONDUCT.md /docs/img/file_screen1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stanbubnoff/logmark/HEAD/docs/img/file_screen1.png -------------------------------------------------------------------------------- /docs/img/file_screen2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stanbubnoff/logmark/HEAD/docs/img/file_screen2.png -------------------------------------------------------------------------------- /docs/img/console_screen1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stanbubnoff/logmark/HEAD/docs/img/console_screen1.png -------------------------------------------------------------------------------- /docs/img/console_screen2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stanbubnoff/logmark/HEAD/docs/img/console_screen2.png -------------------------------------------------------------------------------- /docs/img/console_screen3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stanbubnoff/logmark/HEAD/docs/img/console_screen3.png -------------------------------------------------------------------------------- /src/utils/decorators.js: -------------------------------------------------------------------------------- 1 | export const ANSI_DECORATORS = { 2 | reset: '\u001b[0m', 3 | bold: '\u001b[1m' 4 | } 5 | -------------------------------------------------------------------------------- /docs/img/logmark_primary_color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stanbubnoff/logmark/HEAD/docs/img/logmark_primary_color.png -------------------------------------------------------------------------------- /src/utils/colors.js: -------------------------------------------------------------------------------- 1 | export const ANSI_COLORS = { 2 | debug: '\u001b[38;5;8m', 3 | info: '\u001b[38;5;2m', 4 | warn: '\u001b[38;5;202m', 5 | error: '\u001b[38;5;160m', 6 | fatal: '\u001b[38;5;1m' 7 | } 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Code quality 2 | 3 | All you need is to use the StandardJS (https://github.com/standard/standard) style and SonarLint (https://github.com/SonarSource/sonarlint-vscode). 4 | Welcome to contributors! 5 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | | Version | Supported | 6 | | ------- | ------------------ | 7 | | 1.x.x | :white_check_mark: | 8 | 9 | ## Reporting a Vulnerability 10 | 11 | stasbubnoff@yahoo.com 12 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | import { Logmark } from './pkg/index.js' 2 | 3 | const mark = new Logmark() 4 | 5 | mark.info('some debug message', { 6 | tag: 'server', 7 | data: { important: 'object' } 8 | }) 9 | 10 | mark.warn('some debug message', { 11 | tag: 'server', 12 | data: 'important string' 13 | }) 14 | 15 | mark.debug('some debug message', { 16 | tag: 'server', 17 | data: 10000 18 | }) 19 | 20 | mark.error('some debug message', { 21 | tag: 'server', 22 | error: new Error('error object') 23 | }) 24 | 25 | mark.fatal('some debug message', { 26 | tag: 'server', 27 | error: new Error('fatal error object') 28 | }) 29 | -------------------------------------------------------------------------------- /pkg/index.d.ts: -------------------------------------------------------------------------------- 1 | export declare class Logmark { 2 | constructor(config: Constructor): void 3 | debug(options: Options): void 4 | info(options: Options): void 5 | warn(options: Options): void 6 | error(options: Options): void 7 | fatal(options: Options): void 8 | } 9 | 10 | interface Constructor { 11 | files?: { 12 | enabled?: boolean 13 | pathname?: string 14 | filename?: string 15 | filenames?: { 16 | debug?: string 17 | info?: string 18 | warn?: string 19 | error?: string 20 | fatal?: string 21 | } 22 | } 23 | } 24 | 25 | interface Options { 26 | message: string 27 | options?: { 28 | tag?: string 29 | data?: any 30 | error?: any 31 | } 32 | } -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 Stanislav Bubnov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "logmark", 3 | "version": "1.0.10", 4 | "description": "Simple, lightweight and dependency free nodejs logger with human-readable logs", 5 | "main": "pkg/index.js", 6 | "types": "pkg/index.d.ts", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1", 9 | "lint": "standard src/*/**.js --fix", 10 | "lint-pkg": "standard pkg/index.js --fix", 11 | "build-esm": "esbuild src/core/Logmark.js --outfile=pkg/index.js --bundle --platform=node --format=esm --color=true && lint-pkg", 12 | "build-cjs": "esbuild src/core/Logmark.js --outfile=pkg/index.cjs.js --bundle --platform=node --format=cjs --color=true" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/stasbubnoff/logmark.git" 17 | }, 18 | "author": { 19 | "name": "Stanislav Bubnov", 20 | "email": "stasbubnoff@yahoo.com", 21 | "url": "https://github.com/stasbubnoff" 22 | }, 23 | "contributors": [ 24 | {"name":"Ben Dundon","url":"https://github.com/BenDundon"}, 25 | {"name":"Nikita Galadiy","url":"https://github.com/neki-dev"} 26 | ], 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/stasbubnoff/logmark/issues", 30 | "email": "stasbubnoff@yahoo.com" 31 | }, 32 | "keywords": [ 33 | "terminal", 34 | "console", 35 | "print", 36 | "log", 37 | "logs", 38 | "fast", 39 | "logger", 40 | "logging", 41 | "nodejs", 42 | "color", 43 | "acsii", 44 | "winston", 45 | "pino", 46 | "bunyan", 47 | "loglevel", 48 | "sysadmin", 49 | "monitoring", 50 | "healthcheck" 51 | ], 52 | "homepage": "https://github.com/stasbubnoff/logmark#readme", 53 | "devDependencies": { 54 | "esbuild": "^0.15.13", 55 | "standard": "^17.0.0" 56 | }, 57 | "type": "module" 58 | } 59 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | logs/ 2 | *.log 3 | docs/img/ 4 | *.png 5 | examples 6 | src/ 7 | example.js 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 | # Simple, lightweight (<5Kb) and dependency free nodejs logger with human-readable logs 6 | > ## Why? 7 | > I needed a very simple logger without third-party dependencies, a complex > configuration and with a beautiful human-readable output. 8 | > 9 | > I didn't find it, so I wrote it. 10 | 11 | ## Quick start 12 | 13 | Install the library from npm: 14 | 15 | ``` properties 16 | npm install logmark 17 | ``` 18 | or 19 | ``` properties 20 | yarn add logmark 21 | ``` 22 | Logmark supports five levels of logs: 23 | 24 | - debug 25 | - info 26 | - warn 27 | - error 28 | - fatal 29 | 30 | By default, Logmark generates a new 'logs' folder in your project root, and write all logs to a single `logs.log` file 31 | 32 | Let's look at the code below: 33 | ```javascript 34 | import { Logmark } from 'logmark' 35 | 36 | const mark = new Logmark() 37 | 38 | mark.debug('some debug message') 39 | mark.info('some info message') 40 | mark.warn('some warn message') 41 | mark.error('some error message') 42 | mark.fatal('some fatal message') 43 | ``` 44 | Now we can see next terminal output: 45 | 46 | !['console screen1'](./docs/img/console_screen1.png) 47 | 48 | As you can see, Logmark adds local time, pid and memory usage information to each message. 49 | 50 | I think the memory usage information is very important, and seeing it in logs is very convenient for tracking memory leaks. It may also make it clear early on that refactoring of some functions is necessary. 51 | 52 | Logmark has already created the logs folder and added the `logs.log` file to it. 53 | Let's look at the file output: 54 | 55 | !['file screen1'](./docs/img/file_screen1.png) 56 | 57 | In many cases this is enough for basic use, but you can add a few settings to distribute logs to different files, and you can also attach additional information to messages. 58 | 59 | ## Usage 60 | 61 | ### Config 62 | Let's look how you can set a custom path for the logs folder and file, and distribute logs to different files. 63 | 64 | ```javascript 65 | import { Logmark } from 'logmark' 66 | 67 | const mark = new Logmark({ 68 | files: { 69 | enabled: true, // set the value to false to disable logging to files 70 | pathname: 'custom logs path', 71 | filename: 'custom logs file' 72 | } 73 | }) 74 | ``` 75 | You can also assign a file name for each log level and group logs in any way. 76 | 77 | ```javascript 78 | import { Logmark } from 'logmark' 79 | 80 | const mark = new Logmark({ 81 | files: { 82 | enabled: true, 83 | pathname: 'custom logs path', 84 | // filename: 'custom logs file' 85 | filenames: { 86 | debug: 'डिबग', 87 | info: '资料', 88 | warn: 'atención', 89 | error: 'errors', 90 | fatal: 'errors' 91 | } 92 | } 93 | }) 94 | ``` 95 | 96 | ### Add tag 97 | 98 | You can set a tag to identify the initiator of the message. 99 | ```javascript 100 | import { Logmark } from 'logmark' 101 | 102 | const mark = new Logmark() 103 | 104 | mark.debug('some debug message', { 105 | tag: 'server' 106 | }) 107 | ``` 108 | Console output: 109 | 110 | !['file screen2'](./docs/img/console_screen2.png) 111 | ### Add attachment 112 | You can add an any data or error object to your log message 113 | 114 | ```javascript 115 | import { Logmark } from 'logmark' 116 | 117 | const mark = new Logmark() 118 | 119 | mark.info('some debug message', { 120 | tag: 'server', 121 | data: { important: 'object' } 122 | }) 123 | 124 | mark.warn('some debug message', { 125 | tag: 'server', 126 | data: 'important string' 127 | }) 128 | 129 | mark.debug('some debug message', { 130 | tag: 'server', 131 | data: 10000 132 | }) 133 | 134 | mark.error('some debug message', { 135 | tag: 'server', 136 | error: new Error('error object') 137 | }) 138 | 139 | mark.fatal('some debug message', { 140 | tag: 'server', 141 | error: new Error('fatal error object') 142 | }) 143 | ``` 144 | Console output: 145 | 146 | !['file screen2'](./docs/img/console_screen3.png) 147 | 148 | File output: 149 | 150 | !['file screen2'](./docs/img/file_screen2.png) 151 | 152 | ## Roadmap 153 | 154 | I hope to keep developing the library without compromising on its simplicity and minimalism. If there are suggestions, then forks and pull request are welcome! 155 | 156 | If you would like to see some functionality, you can open an issue. 157 | 158 | I plan to add functionality for daily file rotation in the near future. 159 | 160 | ## Contributing 161 | 162 | Use StandardJS style and SonarLint - contributions are welcome! 163 | 164 | ## Support project 165 | 166 | If you want to support the project with a small donation for my small contribution to open source, I will be very very happy! 167 | 168 | BTC: bc1q4my5g4jf9cy0gzqams9mp06kmyuj59qgpwmrh6 169 | ETH: 0x2C4e8803ecF2E44D55aF615dF8476C786B5b764a 170 | USDT: 0x2C4e8803ecF2E44D55aF615dF8476C786B5b764a 171 | USDC: 0x2C4e8803ecF2E44D55aF615dF8476C786B5b764a 172 | 173 | Also, giving me a simple "star" on GitHub is an easy way to support :) 174 | 175 | Thanks! 176 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | telegram: stasbubnoff. 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | -------------------------------------------------------------------------------- /pkg/index.js: -------------------------------------------------------------------------------- 1 | // src/core/Logmark.js 2 | import fs from 'fs' 3 | 4 | // src/utils/colors.js 5 | const ANSI_COLORS = { 6 | debug: '\x1B[38;5;8m', 7 | info: '\x1B[38;5;2m', 8 | warn: '\x1B[38;5;202m', 9 | error: '\x1B[38;5;160m', 10 | fatal: '\x1B[38;5;1m' 11 | } 12 | 13 | // src/utils/decorators.js 14 | const ANSI_DECORATORS = { 15 | reset: '\x1B[0m', 16 | bold: '\x1B[1m' 17 | } 18 | 19 | // src/core/Logmark.js 20 | const Logmark = class { 21 | #TAB_SYMBOL = '.' 22 | #COLORS = ANSI_COLORS 23 | #DECORS = ANSI_DECORATORS 24 | #MARKERS = { 25 | debug: 'DEBUG', 26 | info: 'INFO', 27 | warn: 'WARN', 28 | error: 'ERROR', 29 | fatal: '! FATAL !' 30 | } 31 | 32 | #FILES_ENABLED = true 33 | #FILES_PATH = 'logs' 34 | #FILES_FILENAME = 'logs' 35 | #FILENAMES = { 36 | debug: null, 37 | info: null, 38 | warn: null, 39 | error: null, 40 | fatal: null 41 | } 42 | 43 | #setFilenames = (config) => { 44 | return { 45 | trace: config.files.filenames.trace ?? this.#FILES_FILENAME, 46 | debug: config.files.filenames.debug ?? this.#FILES_FILENAME, 47 | info: config.files.filenames.info ?? this.#FILES_FILENAME, 48 | warning: config.files.filenames.warning ?? this.#FILES_FILENAME, 49 | error: config.files.filenames.error ?? this.#FILES_FILENAME, 50 | fatal: config.files.filenames.fatal ?? this.#FILES_FILENAME 51 | } 52 | } 53 | 54 | #getMessageHead = () => { 55 | let { rss, heapTotal, heapUsed } = process.memoryUsage() 56 | rss = (rss / 1024).toFixed(2).toString().concat(' Kb') 57 | heapTotal = (heapTotal / 1024).toFixed(2).toString().concat(' Kb') 58 | heapUsed = (heapUsed / 1024).toFixed(2).toString().concat(' Kb') 59 | return `${new Date().toLocaleString()} | pid: ${process.pid} | rss: ${rss} | heapTotal: ${heapTotal} | heapUsed: ${heapUsed} | 60 | 61 | ${this.#TAB_SYMBOL.repeat(3)}` 62 | } 63 | 64 | #createConsoleMessage = (level, msg, options) => { 65 | try { 66 | const extraTag = options && options.tag ? ` #${options.tag}: ` : '' 67 | return `${this.#DECORS.bold}${this.#COLORS[level]}${this.#MARKERS[level]}${this.#DECORS.reset} | ${this.#getMessageHead()} message ${this.#TAB_SYMBOL.repeat(3)} 68 | 69 | ${this.#COLORS[level]}${extraTag}${msg.toString()}${this.#DECORS.reset} 70 | ` 71 | } catch (error) { 72 | console.error(error) 73 | } 74 | } 75 | 76 | #createFileMessage = async (level, msg, options) => { 77 | try { 78 | const extraTag = (options && options.tag) ? ` #${options.tag}: ` : '' 79 | const extraData = (options && options.data) ? `${this.#TAB_SYMBOL.repeat(3)} attachment ${this.#TAB_SYMBOL.repeat(3)}\n\n${JSON.stringify(options.data)}\n\n` : '' 80 | const extraError = (options && options.error) ? `${this.#TAB_SYMBOL.repeat(3)} attachment ${this.#TAB_SYMBOL.repeat(3)}\n\n${(options.error.stack || options.error.message || options.error).toString()}\n\n` : '' 81 | 82 | return `${this.#MARKERS[level]} | ${this.#getMessageHead()} message ${this.#TAB_SYMBOL.repeat(3)}\n\n${extraTag}${msg.toString()}\n\n${extraData || extraError}` 83 | } catch (error) { 84 | console.error(error) 85 | } 86 | } 87 | 88 | #writeToFile = async (level, message) => { 89 | try { 90 | const path = this.#FILENAMES[level] ? `${this.#FILES_PATH}/${this.#FILENAMES[level]}.log` : `${this.#FILES_PATH}/${this.#FILES_FILENAME}.log` 91 | fs.appendFile(path, message, (error) => { 92 | if (error !== null) { 93 | throw new Error(error) 94 | } 95 | }) 96 | } catch (error) { 97 | console.error(error) 98 | } 99 | } 100 | 101 | #log = (level, message, options) => { 102 | try { 103 | const msg = this.#createConsoleMessage(level, message, options ?? {}) 104 | console[level](msg) 105 | if (options && options.data) { 106 | console[level](` 107 | ${this.#TAB_SYMBOL.repeat(3)} attachment ${this.#TAB_SYMBOL.repeat(3)} 108 | `) 109 | console[level](options.data) 110 | console[level]('\n') 111 | } 112 | if (this.#FILES_ENABLED) { 113 | (async () => { 114 | const fileMessage = await this.#createFileMessage(level, message, options) 115 | await this.#writeToFile(level, fileMessage) 116 | })() 117 | } 118 | } catch (error) { 119 | console.error(error) 120 | } 121 | } 122 | 123 | #logError = (level, message, options) => { 124 | try { 125 | const msg = this.#createConsoleMessage(level, message, options ?? {}) 126 | console.error(msg) 127 | if (options && options.error) { 128 | console.error(` 129 | ${this.#TAB_SYMBOL.repeat(3)} attachment ${this.#TAB_SYMBOL.repeat(3)} 130 | `) 131 | console.error(options.error) 132 | console.error('\n') 133 | } 134 | if (this.#FILES_ENABLED) { 135 | (async () => { 136 | const fileMessage = await this.#createFileMessage(level, message, options) 137 | await this.#writeToFile(level, fileMessage) 138 | })() 139 | } 140 | } catch (error) { 141 | console.error(error) 142 | } 143 | } 144 | 145 | /** 146 | * @param {object} [config] 147 | * @param {object} [config.files] 148 | * @param {boolean} [config.files.enabled] 149 | * @param {string} [config.files.pathname] 150 | * @param {string} [config.files.filename] 151 | * @param {object} [config.files.filenames] 152 | * @param {string} [config.files.filenames.debug] 153 | * @param {string} [config.files.filenames.info] 154 | * @param {string} [config.files.filenames.warn] 155 | * @param {string} [config.files.filenames.error] 156 | * @param {string} [config.files.filenames.fatal] 157 | */ 158 | constructor (config) { 159 | try { 160 | if (config && config.files) { 161 | this.#FILES_ENABLED = config.files.enabled ?? this.#FILES_ENABLED 162 | this.#FILES_PATH = config.files.pathname ?? this.#FILES_PATH 163 | this.#FILES_FILENAME = config.files.filename ?? this.#FILES_FILENAME 164 | if (config.files.filenames) { 165 | this.#FILENAMES = this.#setFilenames(config) 166 | } 167 | } 168 | if (this.#FILES_ENABLED) { 169 | fs.mkdir(this.#FILES_PATH, { recursive: true }, (error) => { 170 | if (error !== null) { 171 | console.error(error) 172 | } 173 | }) 174 | } 175 | } catch (error) { 176 | console.error(error) 177 | } 178 | } 179 | 180 | debug (message, options) { 181 | this.#log('debug', message, options) 182 | } 183 | 184 | info (message, options) { 185 | this.#log('info', message, options) 186 | } 187 | 188 | warn (message, options) { 189 | this.#log('warn', message, options) 190 | } 191 | 192 | error (message, options) { 193 | this.#logError('error', message, options) 194 | } 195 | 196 | fatal (message, options) { 197 | this.#logError('fatal', message, options) 198 | } 199 | } 200 | export { 201 | Logmark 202 | } 203 | -------------------------------------------------------------------------------- /src/core/Logmark.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs' 2 | 3 | import { ANSI_COLORS } from '../utils/colors.js' 4 | import { ANSI_DECORATORS } from '../utils/decorators.js' 5 | 6 | /** 7 | * Main class of library 8 | */ 9 | export class Logmark { 10 | // PRIVATE VARS // 11 | 12 | #TAB_SYMBOL = '.' 13 | #COLORS = ANSI_COLORS 14 | #DECORS = ANSI_DECORATORS 15 | 16 | // markers 17 | #MARKERS = { 18 | debug: 'DEBUG', 19 | info: 'INFO', 20 | warn: 'WARN', 21 | error: 'ERROR', 22 | fatal: '! FATAL !' 23 | } 24 | 25 | // files 26 | #FILES_ENABLED = true 27 | #FILES_PATH = 'logs' 28 | #FILES_FILENAME = 'logs' 29 | 30 | #FILENAMES = { 31 | debug: null, 32 | info: null, 33 | warn: null, 34 | error: null, 35 | fatal: null 36 | } 37 | 38 | // PRIVATE METHODS // 39 | #setFilenames = (config) => { 40 | return { 41 | trace: config.files.filenames.trace ?? this.#FILES_FILENAME, 42 | debug: config.files.filenames.debug ?? this.#FILES_FILENAME, 43 | info: config.files.filenames.info ?? this.#FILES_FILENAME, 44 | warning: config.files.filenames.warning ?? this.#FILES_FILENAME, 45 | error: config.files.filenames.error ?? this.#FILES_FILENAME, 46 | fatal: config.files.filenames.fatal ?? this.#FILES_FILENAME 47 | } 48 | } 49 | 50 | #getMessageHead = () => { 51 | let { rss, heapTotal, heapUsed } = process.memoryUsage() 52 | rss = (rss / 1024).toFixed(2).toString().concat(' Kb') 53 | heapTotal = (heapTotal / 1024).toFixed(2).toString().concat(' Kb') 54 | heapUsed = (heapUsed / 1024).toFixed(2).toString().concat(' Kb') 55 | 56 | return `${new Date().toLocaleString()} | pid: ${process.pid} | rss: ${rss} | heapTotal: ${heapTotal} | heapUsed: ${heapUsed} |\n\n${this.#TAB_SYMBOL.repeat(3)}` 57 | } 58 | 59 | #createConsoleMessage = (level, msg, options) => { 60 | try { 61 | const extraTag = (options && options.tag) ? ` #${options.tag}: ` : '' 62 | 63 | return `${this.#DECORS.bold}${this.#COLORS[level]}${this.#MARKERS[level]}${this.#DECORS.reset} | ${this.#getMessageHead()} message ${this.#TAB_SYMBOL.repeat(3)}\n\n${this.#COLORS[level]}${extraTag}${msg.toString()}${this.#DECORS.reset}\n` 64 | } catch (error) { 65 | console.error(error) 66 | } 67 | } 68 | 69 | #createFileMessage = async (level, msg, options) => { 70 | try { 71 | const extraTag = (options && options.tag) ? ` #${options.tag}: ` : '' 72 | const extraData = (options && options.data) ? `${this.#TAB_SYMBOL.repeat(3)} attachment ${this.#TAB_SYMBOL.repeat(3)}\n\n${JSON.stringify(options.data)}\n\n` : '' 73 | const extraError = (options && options.error) ? `${this.#TAB_SYMBOL.repeat(3)} attachment ${this.#TAB_SYMBOL.repeat(3)}\n\n${(options.error.stack || options.error.message || options.error).toString()}\n\n` : '' 74 | 75 | return `${this.#MARKERS[level]} | ${this.#getMessageHead()} message ${this.#TAB_SYMBOL.repeat(3)}\n\n${extraTag}${msg.toString()}\n\n${extraData || extraError}` 76 | } catch (error) { 77 | console.error(error) 78 | } 79 | } 80 | 81 | #writeToFile = async (level, message) => { 82 | try { 83 | const path = this.#FILENAMES[level] ? `${this.#FILES_PATH}/${this.#FILENAMES[level]}.log` : `${this.#FILES_PATH}/${this.#FILES_FILENAME}.log` 84 | 85 | fs.appendFile(path, message, (error) => { 86 | if (error !== null) { 87 | throw new Error(error) 88 | } 89 | }) 90 | } catch (error) { 91 | console.error(error) 92 | } 93 | } 94 | 95 | #log = (level, message, options) => { 96 | try { 97 | const msg = this.#createConsoleMessage(level, message, options ?? {}) 98 | console[level](msg) 99 | 100 | if (options && options.data) { 101 | console[level](`\n${this.#TAB_SYMBOL.repeat(3)} attachment ${this.#TAB_SYMBOL.repeat(3)}\n`) 102 | console[level](options.data) 103 | console[level]('\n') 104 | } 105 | 106 | if (this.#FILES_ENABLED) { 107 | (async () => { 108 | const fileMessage = await this.#createFileMessage(level, message, options) 109 | await this.#writeToFile(level, fileMessage) 110 | })() 111 | } 112 | } catch (error) { 113 | console.error(error) 114 | } 115 | } 116 | 117 | #logError = (level, message, options) => { 118 | try { 119 | const msg = this.#createConsoleMessage(level, message, options ?? {}) 120 | console.error(msg) 121 | 122 | if (options && options.error) { 123 | console.error(`\n${this.#TAB_SYMBOL.repeat(3)} attachment ${this.#TAB_SYMBOL.repeat(3)}\n`) 124 | console.error(options.error) 125 | console.error('\n') 126 | } 127 | 128 | if (this.#FILES_ENABLED) { 129 | (async () => { 130 | const fileMessage = await this.#createFileMessage(level, message, options) 131 | await this.#writeToFile(level, fileMessage) 132 | })() 133 | } 134 | } catch (error) { 135 | console.error(error) 136 | } 137 | } 138 | 139 | /** 140 | * @param {object} [config] 141 | * @param {object} [config.files] 142 | * @param {boolean} [config.files.enabled] 143 | * @param {string} [config.files.pathname] 144 | * @param {string} [config.files.filename] 145 | * @param {object} [config.files.filenames] 146 | * @param {string} [config.files.filenames.debug] 147 | * @param {string} [config.files.filenames.info] 148 | * @param {string} [config.files.filenames.warn] 149 | * @param {string} [config.files.filenames.error] 150 | * @param {string} [config.files.filenames.fatal] 151 | */ 152 | constructor (config) { 153 | try { 154 | if (config && config.files) { 155 | this.#FILES_ENABLED = config.files.enabled ?? this.#FILES_ENABLED 156 | this.#FILES_PATH = config.files.pathname ?? this.#FILES_PATH 157 | this.#FILES_FILENAME = config.files.filename ?? this.#FILES_FILENAME 158 | 159 | if (config.files.filenames) { 160 | this.#FILENAMES = this.#setFilenames(config) 161 | } 162 | } 163 | 164 | if (this.#FILES_ENABLED) { 165 | fs.mkdir(this.#FILES_PATH, { recursive: true }, (error) => { 166 | if (error !== null) { 167 | console.error(error) 168 | } 169 | }) 170 | } 171 | } catch (error) { 172 | console.error(error) 173 | } 174 | } 175 | 176 | /** 177 | * @param {string} message 178 | * @param {object} [options] 179 | * @param {string} [options.tag] 180 | * @param {*} [options.data] 181 | */ 182 | debug (message, options) { 183 | this.#log('debug', message, options) 184 | } 185 | 186 | /** 187 | * @param {string} message 188 | * @param {object} [options] 189 | * @param {string} [options.tag] 190 | * @param {*} [options.data] 191 | */ 192 | info (message, options) { 193 | this.#log('info', message, options) 194 | } 195 | 196 | /** 197 | * @param {string} message 198 | * @param {object} [options] 199 | * @param {string} [options.tag] 200 | * @param {*} [options.data] 201 | */ 202 | warn (message, options) { 203 | this.#log('warn', message, options) 204 | } 205 | 206 | /** 207 | * @param {string} message 208 | * @param {object} [options] 209 | * @param {string} [options.tag] 210 | * @param {*} [options.data] 211 | */ 212 | error (message, options) { 213 | this.#logError('error', message, options) 214 | } 215 | 216 | /** 217 | * @param {string} message 218 | * @param {object} [options] 219 | * @param {string} [options.tag] 220 | * @param {*} [options.data] 221 | */ 222 | fatal (message, options) { 223 | this.#logError('fatal', message, options) 224 | } 225 | } 226 | --------------------------------------------------------------------------------