Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline logging #27

Merged
merged 7 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/hip-years-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"zemble-plugin-auth-otp": patch
"zemble-plugin-auth": patch
"zemble-plugin-cms-users": patch
"zemble-plugin-cms": patch
"@zemble/core": patch
"create-zemble-app": patch
"create-zemble-plugin": patch
"@zemble/graphql": patch
"zemble-plugin-kv": patch
"zemble-plugin-logger-graphql": patch
"minimal": patch
"supplement-stack": patch
---

Streamline logging
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"eslint-config-kingstinct/react-native"
],
"rules": {
"no-console": 1,
"no-console": 2,
"@typescript-eslint/sort-type-union-intersection-members": 0,
"@typescript-eslint/no-unused-vars": 1,
"import/no-unresolved": 0,
Expand Down
25 changes: 9 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ on:
bun-version:
description: 'Bun Version'
required: false
default: 'latest'
type: string
os:
description: 'Operating System'
required: false
default: 'ubuntu-latest'
type: string
mongo-version:
description: 'MongoDB Version'
required: false
default: '7.0.4'
type: string
workflow_dispatch:
inputs:
Expand Down Expand Up @@ -53,16 +50,18 @@ on:
- "**" # matches every branch
- "!main" # excludes the master branch

env:
BUN_VERSION: ${{ inputs.bun-version || 'latest' }}
MONGOMS_PREFER_GLOBAL_PATH: true
MONGOMS_VERSION: ${{ inputs.mongo-version || '7.0.4' }}
RUNS_ON: ${{ inputs.os || 'ubuntu-latest' }}

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
test:
# The type of runner that the job will run on
runs-on: ${{ inputs.os || 'ubuntu-latest' }}
timeout-minutes: 10
env:
MONGOMS_PREFER_GLOBAL_PATH: true
MONGOMS_VERSION: ${{ inputs.mongo-version || '7.0.4' }}
BUN_VERSION: ${{ inputs.bun-version || 'latest' }}

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand All @@ -73,7 +72,7 @@ jobs:
id: cache-mongodb-binaries
name: Cache MongoDB binaries
with:
key: ${{ runner.os }}-mongodb-${{ inputs.bun-version }}-${{ env.MONGOMS_VERSION }}
key: ${{ runner.os }}-mongodb-${{ env.BUN_VERSION }}-${{ env.MONGOMS_VERSION }}
path: ~/.cache/mongodb-binaries

- uses: oven-sh/setup-bun@v1
Expand All @@ -94,9 +93,6 @@ jobs:
env:
SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }}
PROJECT_ID: ${{ secrets.SUPABASE_PROJECT_ID }}
MONGOMS_PREFER_GLOBAL_PATH: true
MONGOMS_VERSION: ${{ inputs.mongo-version || '7.0.4' }}
BUN_VERSION: ${{ inputs.bun-version || 'latest' }}
timeout-minutes: 10
# disable for now - since it for some reason fails on CI
if: false
Expand All @@ -110,7 +106,7 @@ jobs:
id: cache-mongodb-binaries
name: Cache MongoDB binaries
with:
key: ${{ runner.os }}-mongodb-${{ inputs.bun-version }}-${{ env.MONGOMS_VERSION }}
key: ${{ runner.os }}-mongodb-${{ env.BUN_VERSION }}-${{ env.MONGOMS_VERSION }}
path: ~/.cache/mongodb-binaries

- uses: oven-sh/setup-bun@v1
Expand All @@ -125,15 +121,14 @@ jobs:
working-directory: ./packages/supabase/supabase-app

- name: Test
run: bun run codegen && bun test
run: bun test
working-directory: ./packages/supabase

typecheck:
runs-on: ${{ inputs.os || 'ubuntu-latest' }}
timeout-minutes: 10
env:
MONGOMS_DISABLE_POSTINSTALL: true
BUN_VERSION: ${{ inputs.bun-version || 'latest' }}

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand All @@ -156,7 +151,6 @@ jobs:
timeout-minutes: 10
env:
MONGOMS_DISABLE_POSTINSTALL: true
BUN_VERSION: ${{ inputs.bun-version || 'latest' }}

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand All @@ -179,7 +173,6 @@ jobs:
timeout-minutes: 10
env:
MONGOMS_DISABLE_POSTINSTALL: true
BUN_VERSION: ${{ inputs.bun-version || 'latest' }}

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down
4 changes: 4 additions & 0 deletions apps/minimal/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { createApp } from '@zemble/core'
import GraphQL from '@zemble/graphql'
import Migrations from '@zemble/migrations'
import dryrunAdapter from '@zemble/migrations/adapters/dryrun'
import Logger from '@zemble/pino'
import Routes from '@zemble/routes'
import GraphQLLogger from 'zemble-plugin-logger-graphql'

import MyRoutes from './plugins/files/plugin'

export default createApp({
plugins: [
Routes.configure(),
Logger.configure(),
GraphQLLogger,
GraphQL.configure({ }),
MyRoutes.configure(),
Migrations.configure({
Expand Down
9 changes: 3 additions & 6 deletions apps/minimal/graphql/Subscription/randomNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ import type { SubscriptionResolvers } from '../schema.generated'

const randomNumber: SubscriptionResolvers['randomNumber'] = {
// subscribe to the randomNumber event
subscribe: (_, __, { pubsub }) => {
console.log('subscribing to randomNumber')
subscribe: (_, __, { pubsub, logger }) => {
logger.info('subscribing to randomNumber')
return pubsub.subscribe('randomNumber')
},
resolve: (payload: number) => {
console.log('resolving randomNumber', payload)
return payload
},
resolve: (payload: number) => payload,
}

export default randomNumber
4 changes: 2 additions & 2 deletions apps/minimal/graphql/Subscription/tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const initializeOnce = (pubsub: Zemble.PubSubType) => {

const tick: SubscriptionResolvers['tick'] = {
// subscribe to the tick event
subscribe: (_, __, { pubsub }) => {
subscribe: (_, __, { pubsub, logger }) => {
initializeOnce(pubsub)
console.log('subscribing to tick')
logger.info('subscribing to tick')
return pubsub.subscribe('tick')
},
resolve: (payload: number) => payload,
Expand Down
4 changes: 3 additions & 1 deletion apps/minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"@zemble/migrations": "workspace:*",
"@zemble/bun": "workspace:*",
"zemble-plugin-auth": "workspace:*",
"@zemble/routes": "workspace:*"
"@zemble/routes": "workspace:*",
"@zemble/pino": "workspace:*",
"zemble-plugin-logger-graphql": "workspace:*"
},
"devDependencies": {
"@tsconfig/node20": "^20.1.2",
Expand Down
3 changes: 2 additions & 1 deletion apps/supplement-stack/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bunRunner from '@zemble/bun'
import zembleContext from '@zemble/core/zembleContext'
import YogaGraphQL from '@zemble/graphql'
import AuthOTP from 'zemble-plugin-auth-otp'

Expand Down Expand Up @@ -49,4 +50,4 @@ void bunRunner({
],
})

void connect()
void connect({ logger: zembleContext.logger })
13 changes: 7 additions & 6 deletions apps/supplement-stack/clients/papr.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import zembleContext from '@zemble/core/zembleContext'
import { MongoClient } from 'mongodb'
import Papr from 'papr'

import type { IStandardLogger } from '@zemble/core'

// eslint-disable-next-line import/no-mutable-exports
export let client: MongoClient | undefined

const papr = new Papr()

export async function connect() {
export async function connect({ logger }: {readonly logger: IStandardLogger}) {
const mongoUrl = process.env.MONGO_URL

if (!mongoUrl) throw new Error('MONGO_URL not set')

zembleContext.logger.log('Connecting to MongoDB...', mongoUrl)
logger.info('Connecting to MongoDB...', mongoUrl)

client = await MongoClient.connect(mongoUrl)

zembleContext.logger.log('Connected to MongoDB!')
logger.info('Connected to MongoDB!')

const db = client.db()

papr.initialize(db)

zembleContext.logger.log(`Registering ${papr.models.size} models...`)
logger.info(`Registering ${papr.models.size} models...`)
papr.models.forEach((model) => {
zembleContext.logger.log(`Registering model: ${model.collection.collectionName}`)
logger.info(`Registering model: ${model.collection.collectionName}`)
})

await papr.updateSchemas()
Expand Down
4 changes: 2 additions & 2 deletions apps/supplement-stack/graphql/Mutation/addIngredient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { WithoutId } from 'mongodb'

const addIngredient: MutationResolvers['addIngredient'] = async (parent, {
title, imageUrls, nutrientsPer100g, servingSizes, ingredientId,
}) => {
}, { logger }) => {
const _id = ingredientId ? new ObjectId(ingredientId) : new ObjectId()
const ingredient: WithoutId<typeof Ingredient> = {
__typename: 'Ingredient',
Expand All @@ -23,7 +23,7 @@ const addIngredient: MutationResolvers['addIngredient'] = async (parent, {
})) ?? [],
}

console.log(ingredient)
logger.info(ingredient)

const eatable = await Eatables.findOneAndUpdate({ _id }, {
$set: ingredient,
Expand Down
4 changes: 2 additions & 2 deletions apps/todo-app-with-auth-backend/queues/hello-world.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ZembleQueue } from 'zemble-plugin-bull'

export default new ZembleQueue((job) => {
console.log(job.data)
export default new ZembleQueue((job, { logger }) => {
logger.info(job.data)
})
1 change: 1 addition & 0 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import { readdirSync, statSync } from 'node:fs'
import { join } from 'node:path'

Expand Down
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"postinstall": "cp manual-patches/sofa-api/package.json node_modules/sofa-api/",
"pre-push": "bunx turbo run lint test typecheck --continue",
"generate-cms-json-schema": "typescript-json-schema ./packages/cms/tsconfig.json CmsConfigFile --include ./packages/cms/types.ts --out ./packages/cms/entities-json-schema.json --required --strictNullChecks --rejectDateType",
"fix-workspace-dependencies": "find . -type f -name 'package.json' -not -path './package.json' -not -path '*/node_modules/*' -exec perl -pi -e 's/\"workspace:[^\"]*\"/\"*\"/g' {} \\;",
"fix-workspace-dependencies": "find . -type f -name 'package.json' -not -path './package.json' -not -path '*/node_modules/*' -exec perl -pi -e 's/\"workspace:.*\"/\"*\"/g' {} \\;",
"changeset-release": "bun run fix-workspace-dependencies && bunx changeset publish",
"create-changeset": "bunx changeset && bunx changeset version",
"reinstall": "rm -rf node_modules/ && find . -name 'node_modules' -type d -prune -exec rm -rf '{}' + && bun install --force",
Expand Down Expand Up @@ -76,6 +76,7 @@
"eslint-plugin-yml": "^1.11.0",
"husky": "^8.0.3",
"lint-staged": "^15.2.0",
"pino-pretty": "^10.3.1",
"turbo": "^1.11.2",
"typescript-json-schema": "^0.62.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/auth-otp/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const defaultConfig = {
to,
})
} else {
logger.log(`handleAuthRequest for ${to.email}`, twoFactorCode)
logger.info(`handleAuthRequest for ${to.email}`, twoFactorCode)
}
},
} satisfies Partial<OtpAuthConfig>
Expand All @@ -95,7 +95,7 @@ const plugin = new Plugin<OtpAuthConfig, typeof defaultConfig>(import.meta.dir,
],
defaultConfig,
devConfig: {
handleAuthRequest: ({ email }, code, { logger }) => { logger.log(`handleAuthRequest for ${email}`, code) },
handleAuthRequest: ({ email }, code, { logger }) => { logger.info(`handleAuthRequest for ${email}`, code) },
generateTokenContents,
from: { email: 'noreply@zemble.com' },
},
Expand Down
5 changes: 3 additions & 2 deletions packages/auth/bin/generate-keys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bun
import zembleContext from '@zemble/core/zembleContext'
import * as fs from 'node:fs/promises'
import * as path from 'node:path'

Expand All @@ -15,8 +16,8 @@ await generateKeys().then(async ({ publicKey, privateKey }) => {

if (!data.includes('PUBLIC_KEY') && !data.includes('PRIVATE_KEY')) {
await fs.appendFile(envPath, `\nPUBLIC_KEY='${publicKey.trim()}'\nPRIVATE_KEY='${privateKey.trim()}'`)
console.log('PUBLIC_KEY and PRIVATE_KEY was appended to your local .env file!')
zembleContext.logger.info('PUBLIC_KEY and PRIVATE_KEY was appended to your local .env file!')
} else {
console.log('The "PUBLIC_KEY" and/or "PRIVATE_KEY" already exists in .env file, will not overwrite!')
zembleContext.logger.info('The "PUBLIC_KEY" and/or "PRIVATE_KEY" already exists in .env file, will not overwrite!')
}
})
11 changes: 9 additions & 2 deletions packages/bull/ZembleQueueBull.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Queue, Worker } from 'bullmq'

import type { IStandardLogger } from '@zemble/core'
import type {
Job, JobsOptions, QueueOptions, RedisOptions, RepeatOptions,
} from 'bullmq'
Expand All @@ -20,13 +21,19 @@ export type ZembleQueueConfig = {
readonly defaultJobOptions?: JobsOptions
}

export type ZembleJobOpts = {
readonly logger: IStandardLogger
}

export type ZembleWorker<DataType = unknown, ReturnType = unknown> = (job: Job<DataType, ReturnType>, opts: ZembleJobOpts) => Promise<void> | void

export class ZembleQueueBull<DataType = unknown, ReturnType = unknown> {
readonly #worker: (job: Job<DataType, ReturnType>) => Promise<void> | void
readonly #worker: ZembleWorker<DataType, ReturnType>

readonly #config?: ZembleQueueConfig

constructor(
readonly worker: (job: Job<DataType, ReturnType>) => Promise<void> | void,
readonly worker: ZembleWorker<DataType, ReturnType>,
config?: ZembleQueueConfig,
) {
this.#worker = worker
Expand Down
12 changes: 7 additions & 5 deletions packages/bull/ZembleQueueMock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable functional/immutable-data */
/* eslint-disable functional/prefer-readonly-type */

import type { ZembleQueueBull, ZembleQueueConfig } from './ZembleQueueBull'
import zembleContext from '@zemble/core/zembleContext'

import type { ZembleQueueBull, ZembleQueueConfig, ZembleWorker } from './ZembleQueueBull'
import type {
Job, JobsOptions, Queue,
} from 'bullmq'
Expand All @@ -13,14 +15,14 @@ interface IZembleQueue<DataType = unknown, ReturnType = unknown> {

class ZembleQueueMock<DataType = unknown, ReturnType = unknown> implements IZembleQueue<DataType, ReturnType> {
constructor(
readonly worker: (job: Job<DataType, ReturnType>) => Promise<void> | void,
readonly worker: ZembleWorker,
config?: ZembleQueueConfig,
) {
this.#worker = worker
this.#config = config
}

readonly #worker: (job: Job<DataType, ReturnType>) => Promise<void> | void
readonly #worker: ZembleWorker

readonly #config?: ZembleQueueConfig

Expand All @@ -39,7 +41,7 @@ class ZembleQueueMock<DataType = unknown, ReturnType = unknown> implements IZemb
await prev
await new Promise((resolve) => {
setTimeout(() => {
void this.#worker(job)
void this.#worker(job, { logger: zembleContext.logger })
resolve(undefined)
}, 0)
})
Expand Down Expand Up @@ -71,7 +73,7 @@ class ZembleQueueMock<DataType = unknown, ReturnType = unknown> implements IZemb

async add(name: string, data: DataType, opts?: JobsOptions | undefined): Promise<Job<DataType, ReturnType, string>> {
const job = this.#createMockJob(name, data, opts)
setTimeout(async () => this.#worker(job), 0)
setTimeout(async () => this.#worker(job, { logger: zembleContext.logger }), 0)
return job
}
}
Expand Down
Loading