mirror of
https://github.com/pockethost/pockethost.git
synced 2025-06-07 06:36:43 +00:00
feat(instance-logger-file): initial commit
This commit is contained in:
parent
9550c386d9
commit
a1fcc9e217
5
.changeset/1719756120812.md
Normal file
5
.changeset/1719756120812.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
'@pockethost/plugin-instance-logger-file': minor
|
||||
---
|
||||
|
||||
initial commit
|
16
packages/plugin-instance-logger-file/LICENSE.md
Normal file
16
packages/plugin-instance-logger-file/LICENSE.md
Normal file
@ -0,0 +1,16 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or
|
||||
substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
34
packages/plugin-instance-logger-file/package.json
Normal file
34
packages/plugin-instance-logger-file/package.json
Normal file
@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "@pockethost/plugin-instance-logger-file",
|
||||
"version": "0.0.1",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://github.com/pockethost/pockethost/packages/plugin-instance-logger-file"
|
||||
},
|
||||
"description": "A pockethost plugin.",
|
||||
"main": "src/index.ts",
|
||||
"module": "src/index.ts",
|
||||
"types": "src/index.ts",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"check-types": "tsc --noEmit "
|
||||
},
|
||||
"keywords": [
|
||||
"pockethost"
|
||||
],
|
||||
"author": {
|
||||
"name": "Ben Allfree",
|
||||
"url": "https://github.com/benallfree"
|
||||
},
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"pockethost": "workspace:^1.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.8.10",
|
||||
"typescript": "^5.4.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"inflection": "^3.0.0"
|
||||
}
|
||||
}
|
33
packages/plugin-instance-logger-file/readme.md
Normal file
33
packages/plugin-instance-logger-file/readme.md
Normal file
@ -0,0 +1,33 @@
|
||||
# plugin-instance-logger-file
|
||||
|
||||
A plugin for [pockethost](https://www.npmjs.com/package/pockethost).
|
||||
|
||||
## Quickstart
|
||||
|
||||
```bash
|
||||
npx pockethost plugin install @pockethost/plugin-instance-logger-file
|
||||
|
||||
npx pockethost instance-logger-file --help
|
||||
```
|
||||
|
||||
## Variables
|
||||
|
||||
The following variables will be used if they are found in the shell environment. PocketHost will also load them from an `.env` file if found at load time.
|
||||
|
||||
| Name | Default | Discussion |
|
||||
| ------------------------------- | -------------------------------------- | ------------------------------------------------------------- |
|
||||
| PH\_INSTANCE_LOGGER_FILE\_HOME | `.pockethost/plugin-instance-logger-file` | The home directory for any data storage needs of this plugin. |
|
||||
|
||||
## Actions
|
||||
|
||||
## Filters
|
||||
|
||||
## Support
|
||||
|
||||
PocketHost has a thriving [Discord community](https://discord.gg/nVTxCMEcGT).
|
||||
|
||||
---
|
||||
|
||||
### Sponsored by https://pockethost.io. Instantly host your PocketBase projects.
|
||||
|
||||
---
|
22
packages/plugin-instance-logger-file/src/constants.ts
Normal file
22
packages/plugin-instance-logger-file/src/constants.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { underscore } from 'inflection'
|
||||
import { dirname, join } from 'path'
|
||||
import { PH_HOME, Settings, mkPath } from 'pockethost/core'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
export const PLUGIN_NAME = `plugin-instance-logger-file`
|
||||
|
||||
export const HOME_DIR =
|
||||
process.env.PH_INSTANCE_LOGGER_FILE_HOME || join(PH_HOME(),PLUGIN_NAME)
|
||||
|
||||
export const PLUGIN_NAME_CONSTANT_CASE = underscore(PLUGIN_NAME, true)
|
||||
|
||||
export const PLUGIN_DATA = (...paths: string[]) => join(HOME_DIR, ...paths)
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
export const PROJECT_DIR = (...paths: string[]) =>
|
||||
join(__dirname, '..', ...paths)
|
||||
|
||||
export const settings = Settings({
|
||||
PH_INSTANCE_LOGGER_FILE_HOME: mkPath(HOME_DIR, { create: true }),
|
||||
})
|
||||
|
3
packages/plugin-instance-logger-file/src/index.ts
Normal file
3
packages/plugin-instance-logger-file/src/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { plugin } from './plugin'
|
||||
|
||||
export default plugin
|
5
packages/plugin-instance-logger-file/src/log.ts
Normal file
5
packages/plugin-instance-logger-file/src/log.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { LoggerService } from 'pockethost'
|
||||
import { PLUGIN_NAME } from './constants'
|
||||
|
||||
const logger = LoggerService().create(PLUGIN_NAME)
|
||||
export const { dbg, info, error } = logger
|
37
packages/plugin-instance-logger-file/src/plugin.ts
Normal file
37
packages/plugin-instance-logger-file/src/plugin.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { appendFileSync, existsSync, mkdirSync } from 'fs'
|
||||
import {
|
||||
PocketHostPlugin,
|
||||
onAfterPluginsLoadedAction,
|
||||
onInstanceLogAction,
|
||||
onSettingsFilter,
|
||||
} from 'pockethost'
|
||||
import { INSTANCE_DATA_DIR } from 'pockethost/core'
|
||||
import { PLUGIN_NAME, settings } from './constants'
|
||||
import { dbg } from './log'
|
||||
|
||||
export const plugin: PocketHostPlugin = async ({}) => {
|
||||
dbg(`initializing ${PLUGIN_NAME}`)
|
||||
|
||||
onAfterPluginsLoadedAction(async () => {})
|
||||
|
||||
onInstanceLogAction(async ({ instance, type, data }) => {
|
||||
const { id } = instance
|
||||
const logDirectory = INSTANCE_DATA_DIR(id, `logs`)
|
||||
if (!existsSync(logDirectory)) {
|
||||
dbg(`Creating ${logDirectory}`)
|
||||
mkdirSync(logDirectory, { recursive: true })
|
||||
}
|
||||
|
||||
const logFile = INSTANCE_DATA_DIR(id, `logs`, `exec.log`)
|
||||
|
||||
const logLine = JSON.stringify({
|
||||
stream: type,
|
||||
time: +new Date(),
|
||||
message: data,
|
||||
})
|
||||
appendFileSync(logFile, logLine + '\n')
|
||||
dbg(`Logged: ${logLine}`)
|
||||
})
|
||||
|
||||
onSettingsFilter(async (allSettings) => ({ ...allSettings, ...settings }))
|
||||
}
|
20
packages/plugin-instance-logger-file/tsconfig.json
Normal file
20
packages/plugin-instance-logger-file/tsconfig.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": ["ES2021.String"],
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"resolveJsonModule": true,
|
||||
"skipLibCheck": true,
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "node",
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"strictNullChecks": true,
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["**/*.ts"]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user