adapt-authoring-logger 0.0.1

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.
package/.eslintignore ADDED
@@ -0,0 +1 @@
1
+ node_modules
package/.eslintrc ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "env": {
3
+ "browser": false,
4
+ "node": true,
5
+ "commonjs": false,
6
+ "es2020": true
7
+ },
8
+ "extends": [
9
+ "standard"
10
+ ],
11
+ "parserOptions": {
12
+ "ecmaVersion": 2020
13
+ }
14
+ }
@@ -0,0 +1,55 @@
1
+ name: Bug Report
2
+ description: File a bug report
3
+ labels: ["bug"]
4
+ body:
5
+ - type: markdown
6
+ attributes:
7
+ value: |
8
+ Thanks for taking the time to fill out this bug report!
9
+ - type: textarea
10
+ id: description
11
+ attributes:
12
+ label: What happened?
13
+ description: Please describe the issue
14
+ validations:
15
+ required: true
16
+ - type: textarea
17
+ id: expected
18
+ attributes:
19
+ label: Expected behaviour
20
+ description: Tell us what should have happened
21
+ - type: textarea
22
+ id: repro-steps
23
+ attributes:
24
+ label: Steps to reproduce
25
+ description: Tell us how to reproduce the issue
26
+ validations:
27
+ required: true
28
+ - type: input
29
+ id: aat-version
30
+ attributes:
31
+ label: Authoring tool version
32
+ description: What version of the Adapt authoring tool are you running?
33
+ validations:
34
+ required: true
35
+ - type: input
36
+ id: fw-version
37
+ attributes:
38
+ label: Framework version
39
+ description: What version of the Adapt framework are you running?
40
+ - type: dropdown
41
+ id: browsers
42
+ attributes:
43
+ label: What browsers are you seeing the problem on?
44
+ multiple: true
45
+ options:
46
+ - Firefox
47
+ - Chrome
48
+ - Safari
49
+ - Microsoft Edge
50
+ - type: textarea
51
+ id: logs
52
+ attributes:
53
+ label: Relevant log output
54
+ description: Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks.
55
+ render: sh
@@ -0,0 +1 @@
1
+ blank_issues_enabled: false
@@ -0,0 +1,22 @@
1
+ name: Feature request
2
+ description: Request a new feature
3
+ labels: ["enhancement"]
4
+ body:
5
+ - type: markdown
6
+ attributes:
7
+ value: |
8
+ Thanks for taking the time to request a new feature in the Adapt authoring tool! The Adapt team will consider all new feature requests, but unfortunately cannot commit to every one.
9
+ - type: textarea
10
+ id: description
11
+ attributes:
12
+ label: Feature description
13
+ description: Please describe your feature request
14
+ validations:
15
+ required: true
16
+ - type: checkboxes
17
+ id: contribute
18
+ attributes:
19
+ label: Can you work on this feature?
20
+ description: If you are able to commit your own time to work on this feature, it will greatly increase the liklihood of it being implemented by the core dev team. Otherwise, it will be triaged and prioritised alongside the core team's current priorities.
21
+ options:
22
+ - label: I can contribute
@@ -0,0 +1,11 @@
1
+ # To get started with Dependabot version updates, you'll need to specify which
2
+ # package ecosystems to update and where the package manifests are located.
3
+ # Please see the documentation for all configuration options:
4
+ # https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
5
+
6
+ version: 2
7
+ updates:
8
+ - package-ecosystem: "npm" # See documentation for possible values
9
+ directory: "/" # Location of package manifests
10
+ schedule:
11
+ interval: "weekly"
@@ -0,0 +1,25 @@
1
+ [//]: # (Please title your PR according to eslint commit conventions)
2
+ [//]: # (See https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-eslint#eslint-convention for details)
3
+
4
+ [//]: # (Add a link to the original issue)
5
+
6
+ [//]: # (Delete as appropriate)
7
+ ### Fix
8
+ * A sentence describing each fix
9
+
10
+ ### Update
11
+ * A sentence describing each udpate
12
+
13
+ ### New
14
+ * A sentence describing each new feature
15
+
16
+ ### Breaking
17
+ * A sentence describing each breaking change
18
+
19
+ [//]: # (List appropriate steps for testing if needed)
20
+ ### Testing
21
+ 1. Steps for testing
22
+
23
+ [//]: # (Mention any other dependencies)
24
+
25
+
@@ -0,0 +1,16 @@
1
+ name: Add labelled PRs to project
2
+
3
+ on:
4
+ pull_request:
5
+ types: [ labeled ]
6
+
7
+ jobs:
8
+ add-to-project:
9
+ if: ${{ github.event.label.name == 'dependencies' }}
10
+ name: Add to main project
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/add-to-project@v0.1.0
14
+ with:
15
+ project-url: https://github.com/orgs/adapt-security/projects/5
16
+ github-token: ${{ secrets.PROJECTS_SECRET }}
@@ -0,0 +1,19 @@
1
+ name: Add to main project
2
+
3
+ on:
4
+ issues:
5
+ types:
6
+ - opened
7
+ pull_request:
8
+ types:
9
+ - opened
10
+
11
+ jobs:
12
+ add-to-project:
13
+ name: Add to main project
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/add-to-project@v0.1.0
17
+ with:
18
+ project-url: https://github.com/orgs/adapt-security/projects/5
19
+ github-token: ${{ secrets.PROJECTS_SECRET }}
@@ -0,0 +1,6 @@
1
+ {
2
+ "essentialType": "logger",
3
+ "documentation": {
4
+ "enable": true
5
+ }
6
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "type": "object",
4
+ "properties": {
5
+ "levels": {
6
+ "description": "Configures which log levels of log should be shown. Accepts generic levels, module-specific levels and not logic (e.g. 'debug', 'debug.core' and '!debug' respectively).",
7
+ "type": "array",
8
+ "items": { "type": "string" },
9
+ "default": ["error", "warn", "success", "info", "debug"]
10
+ },
11
+ "mute": {
12
+ "description": "Whether to mute log messages",
13
+ "type": ["boolean","string"],
14
+ "default": false
15
+ },
16
+ "dateFormat": {
17
+ "description": "The date format to use",
18
+ "type": "string",
19
+ "enum": ["short", "iso"],
20
+ "default": "iso"
21
+ },
22
+ "showTimestamp": {
23
+ "description": "Whether to prepend a timestamp to each log message",
24
+ "type": "boolean",
25
+ "default": true
26
+ }
27
+ }
28
+ }
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Logging utility
3
+ * @namespace logger
4
+ */
5
+ export { default } from './lib/LoggerModule.js'
@@ -0,0 +1,158 @@
1
+ import { AbstractModule, Hook } from 'adapt-authoring-core'
2
+ import chalk from 'chalk'
3
+ /**
4
+ * Module for logging message to the console
5
+ * @memberof logger
6
+ * @extends {AbstractModule}
7
+ */
8
+ class LoggerModule extends AbstractModule {
9
+ /**
10
+ * Colours an input string
11
+ * @param {String} str
12
+ * @param {String} colour
13
+ * @return {String}
14
+ */
15
+ static colourise (str, colour) {
16
+ const chalkFunc = chalk[colour]
17
+ return chalkFunc ? chalkFunc(str) : str
18
+ }
19
+
20
+ /**
21
+ * Returns a formatted date stamp
22
+ * @param {Object} config
23
+ * @return {String}
24
+ */
25
+ static getDateStamp (config) {
26
+ if (!config.timestamp) {
27
+ return ''
28
+ }
29
+ let str
30
+ if (config.dateFormat === 'iso') {
31
+ str = new Date().toISOString()
32
+ } else if (config.dateFormat === 'short') {
33
+ const d = new Date()
34
+ const m = d.getMonth() + 1
35
+ const s = d.getSeconds()
36
+ const date = `${d.getDate()}/${m < 10 ? `0${m}` : m}/${d.getFullYear().toString().slice(2)}`
37
+ const time = `${d.getHours()}:${d.getMinutes()}:${s < 10 ? `0${s}` : s}`
38
+ str = `${date}-${time}`
39
+ }
40
+ return LoggerModule.colourise(`${str} `, 'grey')
41
+ }
42
+
43
+ /** @override */
44
+ async init () {
45
+ await this.app.waitForModule('config')
46
+ /**
47
+ * Defines what messages are logged
48
+ * @type {Array}
49
+ */
50
+ this.levelsConfig = this.getConfig('levels')
51
+ /**
52
+ * Hook invoked on each message logged
53
+ * @type {Hook}
54
+ */
55
+ this.logHook = new Hook()
56
+ /**
57
+ * Module configuration options
58
+ * @type {Object}
59
+ */
60
+ this.config = {
61
+ levels: {
62
+ error: {
63
+ enable: this.isLevelEnabled('error'),
64
+ moduleOverrides: this.getModuleOverrides('error'),
65
+ colour: 'red'
66
+ },
67
+ warn: {
68
+ enable: this.isLevelEnabled('warn'),
69
+ moduleOverrides: this.getModuleOverrides('warn'),
70
+ colour: 'yellow'
71
+ },
72
+ success: {
73
+ enable: this.isLevelEnabled('success'),
74
+ moduleOverrides: this.getModuleOverrides('success'),
75
+ colour: 'green'
76
+ },
77
+ info: {
78
+ enable: this.isLevelEnabled('info'),
79
+ moduleOverrides: this.getModuleOverrides('info'),
80
+ colour: 'cyan'
81
+ },
82
+ debug: {
83
+ enable: this.isLevelEnabled('debug'),
84
+ moduleOverrides: this.getModuleOverrides('debug'),
85
+ colour: 'grey'
86
+ }
87
+ },
88
+ timestamp: this.getConfig('showTimestamp'),
89
+ dateFormat: this.getConfig('dateFormat'),
90
+ mute: this.getConfig('mute').toString() === 'true'
91
+ }
92
+ if(this.config.levels.mute) this.log('debug', 'logger', 'MUTED')
93
+ else this.log('debug', 'logger', 'CONFIG_LEVELS', JSON.stringify(Object.entries(this.config.levels).map(([k,v]) => `${k}::${v.enable ? 'ENABLED' : 'DISABLED'}::${v.moduleOverrides}`), null, 2))
94
+ // store instance on main App instance
95
+ this.app.logger = this
96
+ }
97
+
98
+ /**
99
+ * Determines whether a specific log level is enabled
100
+ * @param {String} level
101
+ * @return {Boolean}
102
+ */
103
+ isLevelEnabled (level) { // note explicit disable takes preference
104
+ return !this.levelsConfig.includes(`!${level}`) && this.levelsConfig.includes(level)
105
+ }
106
+
107
+ /**
108
+ * Returns a list of log levels with overrides, either inclusive or exclusive
109
+ * @param {String} level
110
+ * @return {Array}
111
+ */
112
+ getModuleOverrides (level) {
113
+ const levels = []
114
+ this.levelsConfig.forEach(l => {
115
+ const s = `${level}.`; const notS = `!${level}.`
116
+ if (l.indexOf(s) === 0 || l.indexOf(notS) === 0) levels.push(l)
117
+ })
118
+ return levels
119
+ }
120
+
121
+ /**
122
+ * Returns whether a message should be logged (i.e. not disabled in the config)
123
+ * @param {string} level Logging level
124
+ * @param {string} id Id of log caller
125
+ * @returns {boolean}
126
+ */
127
+ isLoggingEnabled (level, id) {
128
+ const { enable, moduleOverrides = [] } = this?.config?.levels[level] || {}
129
+ const isEnabled = enable || moduleOverrides.includes(`${level}.${id}`)
130
+ const disableOverride = moduleOverrides.includes(`!${level}.${id}`)
131
+ return isEnabled && !disableOverride
132
+ }
133
+
134
+ /**
135
+ * Logs a message to the console
136
+ * @param {String} level Severity of the message
137
+ * @param {String} id Identifier for the message. Helps to differentiate between other messages.
138
+ * @param {...*} args Arguments to be logged
139
+ */
140
+ log (level, id, ...args) {
141
+ if (this?.config?.mute?.toString() === 'true') {
142
+ return
143
+ }
144
+ if (id === AbstractModule.MODULE_READY) { // @hack for https://github.com/adaptlearning/adapt-authoring/issues/134 (better than overriding setReady)
145
+ id = this.name.split('-').pop()
146
+ args = [AbstractModule.MODULE_READY, ...args]
147
+ }
148
+ if (!this.isLoggingEnabled(level, id)) {
149
+ return
150
+ }
151
+ const colour = this?.config?.levels[level]?.colour
152
+ const logFunc = console[level] ?? console.log
153
+ logFunc(`${LoggerModule.getDateStamp(this.config)}${LoggerModule.colourise(level, colour)} ${LoggerModule.colourise(id, 'magenta')}`, ...args)
154
+ this.logHook.invoke(new Date(), level, id, ...args)
155
+ }
156
+ }
157
+
158
+ export default LoggerModule
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "adapt-authoring-logger",
3
+ "version": "0.0.1",
4
+ "description": "Basic logger for the Adapt authoring tool",
5
+ "homepage": "https://github.com/adapt-security/adapt-authoring-logger",
6
+ "license": "GPL-3.0",
7
+ "type": "module",
8
+ "main": "index.js",
9
+ "repository": "adaptlearning/adapt-authoring-logger",
10
+ "dependencies": {
11
+ "chalk": "^5.3.0"
12
+ },
13
+ "peerDependencies": {
14
+ "adapt-authoring-core": "github:adapt-security/adapt-authoring-core"
15
+ },
16
+ "devDependencies": {
17
+ "eslint": "^9.12.0",
18
+ "standard": "^17.1.0"
19
+ }
20
+ }