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

@sumor/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

@sumor/logger - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

src/code/getI18nConfig.js

12

package.json
{
"name": "@sumor/logger",
"description": "This is a lightweight logger for Node.JS. It can output logs in different levels, and you can customize the scope, id, and timezone.",
"version": "1.1.0",
"version": "1.2.0",
"license": "MIT",

@@ -16,3 +16,4 @@ "repository": "sumor-cloud/logger",

"dependencies": {
"chalk": "^5.3.0"
"chalk": "^5.3.0",
"@sumor/i18n": "^1.0.1"
},

@@ -23,6 +24,9 @@ "devDependencies": {

"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-prettier": "^5.1.3",
"husky": "^9.0.11",
"jest": "^29.7.0",
"jest-html-reporter": "^3.10.2"
"jest-html-reporter": "^3.10.2",
"prettier": "^3.2.5"
},

@@ -47,4 +51,4 @@ "files": [

"prepare": "husky",
"check": "eslint --fix . && npm audit fix --force && npm run coverage"
"check": "eslint --fix . && prettier --write . && npm audit fix --force && npm run coverage"
}
}
# logger
A [Sumor Cloud](https://sumor.cloud) Tool.
[More Documentation](https://sumor.cloud/logger)
[More Documentation](https://sumor.cloud)

@@ -15,2 +15,3 @@ This is a lightweight logger for Node.JS.

## Installation
```bash

@@ -23,10 +24,13 @@ npm i @sumor/logger --save

### Node.JS version
Require Node.JS version 16.x or above
### require Node.JS ES module
As this package is written in ES module,
please change the following code in your ```package.json``` file:
please change the following code in your `package.json` file:
```json
{
"type": "module"
"type": "module"
}

@@ -40,6 +44,6 @@ ```

```js
import Logger from '@sumor/logger';
const logger = new Logger();
import Logger from '@sumor/logger'
const logger = new Logger()
logger.trace("Hello World!");
logger.trace('Hello World!')
// You will see the following output:

@@ -50,9 +54,11 @@ // 2020-01-01 00:00:00.000 TRACE MAIN - Hello World!

### Change Scope
For some case, we need categorize logs. `scope` is used for this purpose.
```js
import Logger from '@sumor/logger';
import Logger from '@sumor/logger'
const logger = new Logger({
scope: 'DEMO'
});
logger.trace("Hello World!");
scope: 'DEMO'
})
logger.trace('Hello World!')
// You will see the following output:

@@ -63,9 +69,11 @@ // 2020-01-01 00:00:00.000 TRACE DEMO - Hello World!

### Identifier User
For some case, we need identifier user. `id` is used for this purpose.
```js
import Logger from '@sumor/logger';
import Logger from '@sumor/logger'
const logger = new Logger({
id: 'USER001'
});
logger.trace("Hello World!");
id: 'USER001'
})
logger.trace('Hello World!')
// You will see the following output:

@@ -76,21 +84,24 @@ // 2020-01-01 00:00:00.000 TRACE MAIN USER001 - Hello World!

### Change Level
Most of the time, we only need to output logs of a certain level. Then we can decide if store and display it or not.
```js
import Logger from '@sumor/logger';
const logger = new Logger();
logger.trace("Hello World!"); // trace is the lowest level, all logs will be output
logger.debug("Hello World!");
logger.info("Hello World!");
logger.warn("Hello World!");
logger.error("Hello World!");
logger.fatal("Hello World!"); // fatal is the highest level, only critical error will be output
import Logger from '@sumor/logger'
const logger = new Logger()
logger.trace('Hello World!') // trace is the lowest level, all logs will be output
logger.debug('Hello World!')
logger.info('Hello World!')
logger.warn('Hello World!')
logger.error('Hello World!')
logger.fatal('Hello World!') // fatal is the highest level, only critical error will be output
```
### Change Timezone
```js
import Logger from '@sumor/logger';
import Logger from '@sumor/logger'
const logger1 = new Logger({
offset: 2 * 60 // UTC+2 offset is 2 hours
});
logger1.info("Hello World!");
offset: 2 * 60 // UTC+2 offset is 2 hours
})
logger1.info('Hello World!')
// You will see the following output:

@@ -100,8 +111,66 @@ // 2020-01-01 02:00:00.000 INFO MAIN - Hello World!

const logger2 = new Logger({
offset: 8 * 60 // UTC+8 offset is 8 hours
});
logger2.info("Hello World!");
offset: 8 * 60 // UTC+8 offset is 8 hours
})
logger2.info('Hello World!')
// You will see the following output:
// 2020-01-01 08:00:00.000 INFO MAIN - Hello World!
```
```
### Predefined Code
```js
import Logger from '@sumor/logger'
const code = {
trace: {
HTTP_ACCESS: 'The user accesses via HTTP and the IP address is {ip}'
},
debug: {
USER_TOKEN_LOADED: 'The user login information is read and the user ID is {id}'
},
info: {
USER_LOGIN: 'The user logs in and the user ID is {id}'
},
warn: {
USER_LOGOUT: 'The user logs out and the user ID is {id}'
},
error: {
USER_LOGIN_FAILED: 'The user login failed and the user ID is {id}'
},
fatal: {
USER_LOGIN_BLOCKED: 'The user login is blocked and the user ID is {id}'
}
}
const i18n = {
zh: {
USER_LOGIN: '用户登录,用户ID为{id}'
}
}
const logger1 = new Logger({
code,
i18n
})
logger1.code('USER_LOGIN', { id: 'USER001' })
// You will see the following output:
// 2020-01-01 00:00:00.000 INFO MAIN - The user logs in and the user ID is USER001
const logger2 = new Logger({
code,
i18n,
language: 'zh-US'
})
logger2.code('USER_LOGIN', { id: 'USER001' })
// You will see the following output:
// 2020-01-01 00:00:00.000 INFO MAIN - The user logs in and the user ID is USER001
const logger3 = new Logger({
code,
i18n,
language: 'zh-CN'
})
logger3.code('USER_LOGIN', { id: 'USER001' })
// You will see the following output:
// 2020-01-01 00:00:00.000 INFO MAIN - 用户登录,用户ID为USER001
```

@@ -1,57 +0,31 @@

import formatPrefix from './formatPrefix.js'
import colorful from './colorful.js'
import filterLevels from './filterLevels.js'
import levels from './levels.js'
import combine from './log/index.js'
import codeUtils from './code/index.js'
export default class Logger {
constructor (options) {
constructor(options) {
options = options || {}
const scope = options.scope
const level = (options.level || 'info').toLowerCase()
const id = options.id
let offset = options.offset
if (options.offset === undefined) {
offset = -new Date().getTimezoneOffset()
}
const types = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']
options.code = options.code || {}
options.i18n = options.i18n || {}
options.language = options.language || 'en-US'
this._codeUtils = codeUtils(options)
const getElementsSinceElement = (arr, ele) => {
const index = arr.indexOf(ele)
return index === -1 ? [] : arr.slice(index)
}
const displayedLevels = getElementsSinceElement(types, level)
for (let i = 0; i < types.length; i += 1) {
const type = types[i]
if (displayedLevels.indexOf(type) === -1) {
this[type] = function () {}
const displayedLevels = filterLevels(options.level)
for (let i = 0; i < levels.length; i += 1) {
const level = levels[i]
if (displayedLevels.indexOf(level) === -1) {
this[level] = function () {}
} else {
this[type] = function () {
const time = Date.now()
const prefix = formatPrefix({
time,
offset,
level: type,
scope,
id
})
const colorfulPrefix = colorful(level, prefix)
const logArray = [colorfulPrefix]
for (let j = 0; j < arguments.length; j += 1) {
logArray.push(arguments[j])
}
this[level] = function () {
const logArray = combine(
{
offset: options.offset,
level,
scope: options.scope,
id: options.id
},
arguments
)
console.log.apply(console, logArray)
try {
let logStr = ''
for (const item of logArray) {
if (typeof item === 'string') {
logStr += item
} else {
logStr += JSON.stringify(item)
}
}
return logStr
} catch (e) {
return undefined
}
}

@@ -61,2 +35,11 @@ }

}
code(code, parameters) {
const { level, message } = this._codeUtils(code, parameters)
if (levels.indexOf(level) > -1) {
this[level](code, message)
} else {
this.error(code, parameters)
}
}
}
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