@qubit-ltd/logging 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +224 -0
  3. package/README.zh_CN.md +200 -0
  4. package/dist/logging.cjs +1338 -0
  5. package/dist/logging.cjs.map +1 -0
  6. package/dist/logging.iife.js +1342 -0
  7. package/dist/logging.iife.js.map +1 -0
  8. package/dist/logging.min.cjs +1 -0
  9. package/dist/logging.min.cjs.map +1 -0
  10. package/dist/logging.min.iife.js +1 -0
  11. package/dist/logging.min.iife.js.map +1 -0
  12. package/dist/logging.min.mjs +1 -0
  13. package/dist/logging.min.mjs.map +1 -0
  14. package/dist/logging.mjs +1330 -0
  15. package/dist/logging.mjs.map +1 -0
  16. package/doc/api/Logger.html +3352 -0
  17. package/doc/api/fonts/OpenSans-Bold-webfont.eot +0 -0
  18. package/doc/api/fonts/OpenSans-Bold-webfont.svg +1838 -0
  19. package/doc/api/fonts/OpenSans-Bold-webfont.woff +0 -0
  20. package/doc/api/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  21. package/doc/api/fonts/OpenSans-BoldItalic-webfont.svg +1838 -0
  22. package/doc/api/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  23. package/doc/api/fonts/OpenSans-Italic-webfont.eot +0 -0
  24. package/doc/api/fonts/OpenSans-Italic-webfont.svg +1838 -0
  25. package/doc/api/fonts/OpenSans-Italic-webfont.woff +0 -0
  26. package/doc/api/fonts/OpenSans-Light-webfont.eot +0 -0
  27. package/doc/api/fonts/OpenSans-Light-webfont.svg +1839 -0
  28. package/doc/api/fonts/OpenSans-Light-webfont.woff +0 -0
  29. package/doc/api/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  30. package/doc/api/fonts/OpenSans-LightItalic-webfont.svg +1843 -0
  31. package/doc/api/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  32. package/doc/api/fonts/OpenSans-Regular-webfont.eot +0 -0
  33. package/doc/api/fonts/OpenSans-Regular-webfont.svg +1839 -0
  34. package/doc/api/fonts/OpenSans-Regular-webfont.woff +0 -0
  35. package/doc/api/fonts/OpenSans-Semibold-webfont.eot +0 -0
  36. package/doc/api/fonts/OpenSans-Semibold-webfont.svg +1838 -0
  37. package/doc/api/fonts/OpenSans-Semibold-webfont.ttf +0 -0
  38. package/doc/api/fonts/OpenSans-Semibold-webfont.woff +0 -0
  39. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.eot +0 -0
  40. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.svg +1838 -0
  41. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.ttf +0 -0
  42. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.woff +0 -0
  43. package/doc/api/global.html +787 -0
  44. package/doc/api/index.html +257 -0
  45. package/doc/api/scripts/linenumber.js +34 -0
  46. package/doc/api/scripts/prettify/Apache-License-2.0.txt +202 -0
  47. package/doc/api/scripts/prettify/lang-css.js +2 -0
  48. package/doc/api/scripts/prettify/prettify.js +28 -0
  49. package/doc/api/styles/jsdoc-default.css +699 -0
  50. package/doc/api/styles/prettify-jsdoc.css +120 -0
  51. package/doc/api/styles/prettify-tomorrow.css +141 -0
  52. package/doc/logging.min.visualization.html +4949 -0
  53. package/doc/logging.visualization.html +4949 -0
  54. package/package.json +103 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,224 @@
1
+ # js-logging
2
+
3
+ [![npm package](https://img.shields.io/npm/v/@qubit-ltd/logging.svg)](https://npmjs.com/package/@qubit-ltd/logging)
4
+ [![License](https://img.shields.io/badge/License-Apache-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
5
+ [![中文文档](https://img.shields.io/badge/文档-中文版-blue.svg)](README.zh_CN.md)
6
+ [![CircleCI](https://dl.circleci.com/status-badge/img/gh/Haixing-Hu/js-logging/tree/master.svg?style=shield)](https://dl.circleci.com/status-badge/redirect/gh/Haixing-Hu/js-logging/tree/master)
7
+ [![Coverage Status](https://coveralls.io/repos/github/Haixing-Hu/js-logging/badge.svg?branch=master)](https://coveralls.io/github/Haixing-Hu/js-logging?branch=master)
8
+
9
+ [@qubit-ltd/logging] is a JavaScript library that provides powerful
10
+ logging capabilities through decorators for class methods and properties.
11
+ This library is designed to seamlessly integrate with [Vue.js class components],
12
+ offering an elegant solution for handling logging in your JavaScript projects.
13
+
14
+ ## Installation
15
+
16
+ To install the library, use either npm or yarn:
17
+ ```sh
18
+ npm install @qubit-ltd/logging
19
+ ```
20
+ or
21
+ ```sh
22
+ yarn add @qubit-ltd/logging
23
+ ```
24
+
25
+ ## The `Logger` Class
26
+
27
+ The `Logger` class provides a simple yet flexible logging interface.
28
+
29
+ ### Get or create a Logger
30
+
31
+ You can retrieve a `Logger` instance by calling the static method
32
+ `Logger.getLogger(name, options)`, where
33
+ - `name` is the identifier of the logger. If a logger with the same name exists,
34
+ it will be returned; otherwise, a new one will be created.
35
+ - `options` (optional) is an object that may include:
36
+ - `appender: object`: specifies the output destination for log messages.
37
+ This object must implement `trace`, `debug`, `info`, `warn` and `error`
38
+ methods. If omitted, the existing appender of the logger will be used, or
39
+ the default appender will be assigned to a new logger.
40
+ - `level: string`: defines the logging level (`TRACE`, `DEBUG`, `INFO`, `WARN`,
41
+ `ERROR`, `NONE`). Case-insensitive. If omitted, the existing logging level
42
+ of the logger will be used, or the default logging level will be assigned to
43
+ a new logger.
44
+
45
+ ### Logging Messages
46
+
47
+ - `logger.trace(message, ...args)`: Logs a trace-level message.
48
+ - `logger.debug(message, ...args)`: Logs a debug-level message.
49
+ - `logger.info(message, ...args)`: Logs an info-level message.
50
+ - `logger.warn(message, ...args)`: Logs a warning-level message.
51
+ - `logger.error(message, ...args)`: Logs an error-level message.
52
+ - `logger.log(level, message, ...args)`: Logs a message in the specified logging level.
53
+
54
+ You can use placeholders in log messages to dynamically insert variables:
55
+
56
+ - `%o` or `%O`: JavaScript object output. Clicking the object name opens
57
+ more information about it in the inspector.
58
+ - `%d` or` %i`: Integer output (supports formatting). For example,
59
+ `logger.info('Foo %.2d', 1.1)` will output the number as two significant
60
+ figures with a leading 0: `Foo 01`.
61
+ - `%s`: String output.
62
+ - `%f`: Floating-point number output (supports formatting). For example,
63
+ `logger.debug("Foo %.2f", 1.1)` will output the number to 2 decimal
64
+ places: `Foo 1.10`.
65
+
66
+ Example:
67
+
68
+ ```javascript
69
+ import Logger from '@qubit-ltd/logging';
70
+
71
+ const logger = Logger.getLogger('MyClass');
72
+ logger.trace('This is a trace message with argument %s and argument %o', 'foo', { bar: 'baz' });
73
+ logger.debug('This is a debug message with argument %s and argument %o', 'foo', { bar: 'baz' });
74
+ logger.info('This is an info message with argument %s and argument %o', 'foo', { bar: 'baz' });
75
+ logger.warn('This is a warning message with argument %s and argument %o', 'foo', { bar: 'baz' });
76
+ logger.error('This is an error message with argument %s and argument %o', 'foo', { bar: 'baz' });
77
+ const level = 'info';
78
+ logger.log(level, 'This is an %s message with argument %s and argument %o', level, 'foo', { bar: 'baz' });
79
+ ```
80
+
81
+ ### Set the Logging Level
82
+
83
+ Adjust the logging level for a logger using `logger.setLevel(level)`.
84
+
85
+ Available levels: `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `NONE` (case-insensitive).
86
+
87
+ ### Set the Logging Appender
88
+
89
+ Use `logger.setAppender(appender)` to assign a custom appender object that defines:
90
+ - `trace(message, ...args)`
91
+ - `debug(message, ...args)`
92
+ - `info(message ...args)`
93
+ - `warn(message, ...args)`
94
+ - `error(message, ...args)`
95
+
96
+ Example:
97
+
98
+ ```javascript
99
+ const logger = Logger.getLogger('MyClass');
100
+ logger.setAppender(console); // Outputs log messages to the console.
101
+ ```
102
+
103
+ ### Enable or Disable Logging
104
+
105
+ - `logger.enable()`: Enable logging.
106
+ - `logger.disable()`: Disable logging.
107
+ - `logger.setEnabled(enabled)`: Dynamically control logging.
108
+
109
+ ### Managing Loggers
110
+
111
+ - `Logger.clearAllLoggers()`: Clears all registered loggers.
112
+ - `Logger.getLevel(name)`: Retrieves the logging level for a specific logger.
113
+ - `Logger.setLevel(name, level)`: Sets the logging level for a specific logger.
114
+
115
+ ### Default Levels and Appenders
116
+
117
+ The default logging levels and appenders are used when creating a new logger
118
+ without specifying the level or appender.
119
+
120
+ - `Logger.getDefaultLevel()`: Gets the default logging level.
121
+ - `Logger.setDefaultLevel(level)`: Sets the default logging level.
122
+ - `Logger.resetDefaultLevel()`: Resets the default logging level to the
123
+ factory value.
124
+ - `Logger.getDefaultAppender()`: Gets the default logging appender.
125
+ - `Logger.setDefaultAppender(appender)`: Sets the default logging appender.
126
+ - `Logger.resetDefaultAppender()`: Resets the default logging appender to the
127
+ factory value.
128
+
129
+ ### Global Loggers Management
130
+
131
+ - `Logger.setAllLevels(level)`: Applies a logging level to all existing loggers.
132
+ - `Logger.resetAllLevels()`: Resets the logging level of all existing loggers to
133
+ the default logging level.
134
+ - `Logger.setAllAppenders(appender)`: Applies a logging appender to all existing
135
+ loggers.
136
+ - `Logger.resetAllAppenders()`: Resets the logging appender of all existing loggers
137
+ to the default logging appender.
138
+
139
+ ### Reset to Factory Defaults
140
+
141
+ - `Logger.reset()`: Resets all loggers to the factory default settings. This
142
+ includes clearing all existing loggers, and resetting the default logging
143
+ level and the default logging appender.
144
+
145
+ ## The `@Log` Decorator
146
+
147
+ The `@Log` decorator automatically logs the method signature, including the
148
+ class name, method name, and parameters.
149
+
150
+ Example:
151
+
152
+ ```javascript
153
+ import { Log } from '@qubit-ltd/logging';
154
+
155
+ class Person {
156
+ @Log
157
+ eat(meal) {
158
+ // method implementation
159
+ }
160
+ }
161
+
162
+ const person = new Person();
163
+ const meal = new Meal();
164
+ person.eat(meal); // The log will print the method calling signature
165
+ ```
166
+
167
+ ## The `@HasLogger` Decorator
168
+
169
+ The `@HasLogger` decorator adds a named logger to a class, which is accessibl
170
+ via the `logger` property.
171
+
172
+ Example:
173
+
174
+ ```javascript
175
+ import { HasLogger } from '@qubit-ltd/logging';
176
+
177
+ @HasLogger
178
+ class MyClass {
179
+ foo() {
180
+ this.logger.debug('This is MyClass.foo()');
181
+ }
182
+ }
183
+ ```
184
+
185
+ ## Using with Vue.js Class Components
186
+
187
+ You can use the `@Log` and `@HasLogger` decorators with [Vue.js class components]:
188
+
189
+ ```javascript
190
+ import { HasLogger, Log } from '@qubit-ltd/logging';
191
+ import { Component, toVue } from '@qubit-ltd/vue3-class-component';
192
+
193
+ @Component({
194
+ template: '<p @click="foo">{{ message }}</p>',
195
+ })
196
+ @HasLogger
197
+ class MyComponent {
198
+
199
+ message = 'hello world';
200
+
201
+ @Log
202
+ foo() {
203
+ this.logger.debug('This is MyComponent.foo()');
204
+ }
205
+ }
206
+
207
+ export default toVue(MyComponent);
208
+ ```
209
+
210
+ **Note**: The `@HasLogger` decorator must be placed **after** the `@Component` decorator.
211
+
212
+ ## <span id="contributing">Contributing</span>
213
+
214
+ If you find any issues or have suggestions for improvements, please feel free
215
+ to open an issue or submit a pull request to the [GitHub repository].
216
+
217
+ ## <span id="license">License</span>
218
+
219
+ [@qubit-ltd/logging] is distributed under the Apache 2.0 license.
220
+ See the [LICENSE](LICENSE) file for more details.
221
+
222
+ [@qubit-ltd/logging]: https://npmjs.com/package/@qubit-ltd/logging
223
+ [Vue.js class components]: https://github.com/Haixing-Hu/vue3-class-component/
224
+ [GitHub repository]: https://github.com/Haixing-Hu/js-logging
@@ -0,0 +1,200 @@
1
+ # js-logging
2
+
3
+ [![npm package](https://img.shields.io/npm/v/@qubit-ltd/logging.svg)](https://npmjs.com/package/@qubit-ltd/logging)
4
+ [![License](https://img.shields.io/badge/License-Apache-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
5
+ [![English Document](https://img.shields.io/badge/Document-English-blue.svg)](README.md)
6
+ [![CircleCI](https://dl.circleci.com/status-badge/img/gh/Haixing-Hu/js-logging/tree/master.svg?style=shield)](https://dl.circleci.com/status-badge/redirect/gh/Haixing-Hu/js-logging/tree/master)
7
+ [![Coverage Status](https://coveralls.io/repos/github/Haixing-Hu/js-logging/badge.svg?branch=master)](https://coveralls.io/github/Haixing-Hu/js-logging?branch=master)
8
+
9
+ [@qubit-ltd/logging] 是一个 JavaScript 库,通过装饰器为类方法和属性提供强大的日志记录功能。
10
+ 该库旨在与[Vue.js 类组件]无缝集成,为处理 JavaScript 项目中的日志记录提供了优雅的解决方案。
11
+
12
+ ## 安装
13
+
14
+ 使用 npm 或 yarn 安装该库:
15
+ ```sh
16
+ npm install @qubit-ltd/logging
17
+ ```
18
+
19
+ ```sh
20
+ yarn add @qubit-ltd/logging
21
+ ```
22
+
23
+ ## `Logger` 类
24
+
25
+ `Logger` 类提供了一个简单而灵活的日志记录接口。
26
+
27
+ ### 获取或创建 Logger
28
+
29
+ 你可以通过调用静态方法 `Logger.getLogger(name, options)` 获取一个 `Logger` 实例,其中:
30
+ - `name` 是 logger 的标识符。如果已经存在具有相同名称的 logger,则返回该实例;否则将创建一个新的 logger。
31
+ - `options`(可选)是一个对象,可能包括:
32
+ - `appender: object`:指定日志消息的输出目的地。此对象必须实现 `trace`、`debug`、`info`、`warn` 和 `error` 方法。
33
+ 如果省略,将使用 logger 的现有 appender,或者为新创建的 logger 分配默认 appender。
34
+ - `level: string`:定义日志记录级别(`TRACE`、`DEBUG`、`INFO`、`WARN`、`ERROR`、`NONE`)。不区分大小写。
35
+ 如果省略,将使用 logger 的现有日志级别,或者为新创建的 logger 分配默认日志级别。
36
+
37
+ ### 记录日志消息
38
+
39
+ - `logger.trace(message, ...args)`:记录一个 trace 级别的消息。
40
+ - `logger.debug(message, ...args)`:记录一个 debug 级别的消息。
41
+ - `logger.info(message, ...args)`:记录一个 info 级别的消息。
42
+ - `logger.warn(message, ...args)`:记录一个警告级别的消息。
43
+ - `logger.error(message, ...args)`:记录一个错误级别的消息。
44
+ - `logger.log(level, message, ...args)`:以指定的日志级别记录消息。
45
+
46
+ 你可以在日志消息中使用占位符动态插入变量:
47
+
48
+ - `%o` 或 `%O`:JavaScript 对象输出。点击对象名称可以在检查器中查看更多信息。
49
+ - `%d` 或 `%i`:整数输出(支持格式化)。例如,`logger.info('Foo %.2d', 1.1)` 将数字输出为两位有效数字并带有前导0:
50
+ `Foo 01`。
51
+ - `%s`:字符串输出。
52
+ - `%f`:浮点数输出(支持格式化)。例如,`logger.debug("Foo %.2f", 1.1)` 将数字输出为两位小数:`Foo 1.10`。
53
+
54
+ 示例:
55
+
56
+ ```javascript
57
+ import Logger from '@qubit-ltd/logging';
58
+
59
+ const logger = Logger.getLogger('MyClass');
60
+ logger.trace('This is a trace message with argument %s and argument %o', 'foo', { bar: 'baz' });
61
+ logger.debug('This is a debug message with argument %s and argument %o', 'foo', { bar: 'baz' });
62
+ logger.info('This is an info message with argument %s and argument %o', 'foo', { bar: 'baz' });
63
+ logger.warn('This is a warning message with argument %s and argument %o', 'foo', { bar: 'baz' });
64
+ logger.error('This is an error message with argument %s and argument %o', 'foo', { bar: 'baz' });
65
+ const level = 'info';
66
+ logger.log(level, 'This is an %s message with argument %s and argument %o', level, 'foo', { bar: 'baz' });
67
+ ```
68
+
69
+ ### 设置日志级别
70
+
71
+ 使用 `logger.setLevel(level)` 调整 logger 的日志级别。
72
+
73
+ 可用的日志级别有:`TRACE`、`DEBUG`、`INFO`、`WARN`、`ERROR`、`NONE`(不区分大小写)。
74
+
75
+ ### 设置日志 Appender
76
+
77
+ 使用 `logger.setAppender(appender)` 为 logger 分配一个自定义的 appender 对象,该对象定义了以下方法:
78
+ - `trace(message, ...args)`
79
+ - `debug(message, ...args)`
80
+ - `info(message ...args)`
81
+ - `warn(message, ...args)`
82
+ - `error(message, ...args)`
83
+
84
+ 示例:
85
+
86
+ ```javascript
87
+ const logger = Logger.getLogger('MyClass');
88
+ logger.setAppender(console); // Outputs log messages to the console.
89
+ ```
90
+
91
+ ### 启用或禁用日志记录
92
+
93
+ - `logger.enable()`:启用日志记录。
94
+ - `logger.disable()`:禁用日志记录。
95
+ - `logger.setEnabled(enabled)`:动态控制日志记录的启用与禁用。
96
+
97
+ ### 管理日志记录器
98
+
99
+ - `Logger.clearAllLoggers()`:清除所有已注册的日志记录器。
100
+ - `Logger.getLevel(name)`:获取特定日志记录器的日志级别。
101
+ - `Logger.setLevel(name, level)`:设置特定日志记录器的日志级别。
102
+
103
+ ### 默认级别和 Appender
104
+
105
+ 当创建一个新日志记录器时,如果没有指定级别或 appender,将使用默认的日志级别和 appender。
106
+
107
+ - `Logger.getDefaultLevel()`:获取默认日志级别。
108
+ - `Logger.setDefaultLevel(level)`:设置默认日志级别。
109
+ - `Logger.resetDefaultLevel()`:将默认日志级别重置为出厂值。
110
+ - `Logger.getDefaultAppender()`:获取默认日志 appender。
111
+ - `Logger.setDefaultAppender(appender)`:设置默认日志 appender。
112
+ - `Logger.resetDefaultAppender()`:将默认日志 appender 重置为出厂值。
113
+
114
+ ### 全局日志管理
115
+
116
+ - `Logger.setAllLevels(level)`:将指定日志级别应用于所有现有日志记录器。
117
+ - `Logger.resetAllLevels()`:将所有现有日志记录器的日志级别重置为默认日志级别。
118
+ - `Logger.setAllAppenders(appender)`:将指定日志 appender 应用于所有现有日志记录器。
119
+ - `Logger.resetAllAppenders()`:将所有现有日志记录器的日志 appender 重置为默认 appender。
120
+
121
+ ### 重置日志记录器
122
+
123
+ - `Logger.reset()`:将日志记录器重置为出厂状态。这将清除所有已注册的日志记录器、重置默认日志级别和默认日志 appender。
124
+
125
+ ## `@Log` 装饰器
126
+
127
+ `@Log` 装饰器会自动记录方法签名,包括类名、方法名和参数。
128
+
129
+ 示例:
130
+
131
+ ```javascript
132
+ import { Log } from '@qubit-ltd/logging';
133
+
134
+ class Person {
135
+ @Log
136
+ eat(meal) {
137
+ // method implementation
138
+ }
139
+ }
140
+
141
+ const person = new Person();
142
+ const meal = new Meal();
143
+ person.eat(meal); // The log will print the method calling signature
144
+ ```
145
+
146
+ ## `@HasLogger` 装饰器
147
+
148
+ `@HasLogger` 装饰器会为类添加一个命名的日志记录器,可以通过 `logger` 属性访问。
149
+
150
+ 示例:
151
+
152
+ ```javascript
153
+ import { HasLogger } from '@qubit-ltd/logging';
154
+
155
+ @HasLogger
156
+ class MyClass {
157
+ foo() {
158
+ this.logger.debug('This is MyClass.foo()');
159
+ }
160
+ }
161
+ ```
162
+
163
+ ## 与 Vue.js 类组件一起使用
164
+
165
+ 你可以在[Vue.js 类组件]中使用 `@Log` 和 `@HasLogger` 装饰器:
166
+
167
+ ```javascript
168
+ import { HasLogger, Log } from '@qubit-ltd/logging';
169
+ import { Component, toVue } from '@qubit-ltd/vue3-class-component';
170
+
171
+ @Component({
172
+ template: '<p @click="foo">{{ message }}</p>',
173
+ })
174
+ @HasLogger
175
+ class MyComponent {
176
+
177
+ message = 'hello world';
178
+
179
+ @Log
180
+ foo() {
181
+ this.logger.debug('This is MyComponent.foo()');
182
+ }
183
+ }
184
+
185
+ export default toVue(MyComponent);
186
+ ```
187
+
188
+ **注意**:`@HasLogger` 装饰器必须放在 `@Component` 装饰器的**后面**。
189
+
190
+ ## <span id="contributing">贡献</span>
191
+
192
+ 如果您发现任何问题或有改进建议,请随时在[GitHub 仓库]上提交 issue 或 pull request。
193
+
194
+ ## <span id="license">许可证</span>
195
+
196
+ [@qubit-ltd/logging] 根据 Apache 2.0 许可证分发。详情请参阅 [LICENSE](LICENSE) 文件。
197
+
198
+ [@qubit-ltd/logging]: https://npmjs.com/package/@qubit-ltd/logging
199
+ [Vue.js 类组件]: https://github.com/Haixing-Hu/vue3-class-component/
200
+ [GitHub 仓库]: https://github.com/Haixing-Hu/js-logging