diff --git a/.changeset/pink-needles-sing.md b/.changeset/pink-needles-sing.md new file mode 100644 index 00000000..02f0d400 --- /dev/null +++ b/.changeset/pink-needles-sing.md @@ -0,0 +1,6 @@ +--- +"@cipherstash/nextjs": minor +"@cipherstash/jseql": minor +--- + +Implement packageless logging framework. diff --git a/README.md b/README.md index e9cb767b..c43f4c5d 100644 --- a/README.md +++ b/README.md @@ -423,15 +423,13 @@ You can read more about this feature and implementation [here](https://github.co > `jseql` will NEVER log plaintext data. > This is by design to prevent sensitive data from being logged. -`@cipherstash/jseql` and `@cipherstash/nextjs` use [node's built-in debug logging](https://nodejs.org/api/util.html#utildebuglogsection-callback) for logging. - -By default, the logger is disabled, but you can enable it by configuring the following environment variables: +`@cipherstash/jseql` and `@cipherstash/nextjs` will log to the console with a log level of `info` by default. +You can enable the logger by configuring the following environment variable: ```bash -NODE_DEBUG=jseql-debug # Enable debug logging -NODE_DEBUG=jseql-error # Enable error logging -NODE_DEBUG=jseql-info # Enable info logging -NODE_DEBUG=jseql-* # Enable all logging +JSEQL_LOG_LEVEL=debug # Enable debug logging +JSEQL_LOG_LEVEL=info # Enable info logging +JSEQL_LOG_LEVEL=error # Enable error logging ``` ## Examples diff --git a/packages/jseql/src/logger/index.ts b/packages/jseql/src/logger/index.ts index 77c73e92..864dae1c 100644 --- a/packages/jseql/src/logger/index.ts +++ b/packages/jseql/src/logger/index.ts @@ -1,14 +1,39 @@ -import { debuglog } from 'node:util' -type LoggerFunction = (...args: unknown[]) => void +function getLevelValue(level: string): number { + switch (level) { + case 'debug': + return 10 + case 'info': + return 20 + case 'error': + return 30 + default: + return 20 // default to 'info' + } +} + +const envLogLevel = process.env.JSEQL_LOG_LEVEL || 'info' +const currentLevel = getLevelValue(envLogLevel) + +function debug(...args: unknown[]): void { + if (currentLevel <= getLevelValue('debug')) { + console.debug('[DEBUG]', ...args) + } +} + +function info(...args: unknown[]): void { + if (currentLevel <= getLevelValue('info')) { + console.info('[INFO]', ...args) + } +} -const log = { - debug: debuglog('jseql-debug') as LoggerFunction, - error: debuglog('jseql-error') as LoggerFunction, - info: debuglog('jseql-info') as LoggerFunction, +function error(...args: unknown[]): void { + if (currentLevel <= getLevelValue('error')) { + console.error('[ERROR]', ...args) + } } export const logger = { - debug: (...args: unknown[]) => log.debug(...args), - error: (...args: unknown[]) => log.error(...args), - info: (...args: unknown[]) => log.info(...args), + debug, + info, + error, } diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index 840ecc5b..c3314d3c 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -35,7 +35,6 @@ "dev": "tsup --watch" }, "devDependencies": { - "@cipherstash/jseql": "workspace:*", "@clerk/nextjs": "^6.9.7", "dotenv": "^16.4.7", "tsup": "^8.3.0", diff --git a/packages/nextjs/src/clerk/index.ts b/packages/nextjs/src/clerk/index.ts index 7abc5d1d..c277b2c0 100644 --- a/packages/nextjs/src/clerk/index.ts +++ b/packages/nextjs/src/clerk/index.ts @@ -1,8 +1,7 @@ import type { ClerkMiddlewareAuth } from '@clerk/nextjs/server' import type { NextRequest } from 'next/server' import { NextResponse } from 'next/server' -import { CS_COOKIE_NAME } from '../index' -import type { CtsToken } from '@cipherstash/jseql' +import { CS_COOKIE_NAME, type CtsToken } from '../index' import { logger } from '../logger' export const jseqlClerkMiddleware = async ( diff --git a/packages/nextjs/src/index.ts b/packages/nextjs/src/index.ts index 15c44f8c..e2953230 100644 --- a/packages/nextjs/src/index.ts +++ b/packages/nextjs/src/index.ts @@ -1,9 +1,13 @@ import { cookies } from 'next/headers' -import type { CtsToken } from '@cipherstash/jseql' import { logger } from './logger' export const CS_COOKIE_NAME = '__cipherstash_cts_session' +export type CtsToken = { + accessToken: string + expiry: number +} + export const getCtsToken = async () => { const cookieStore = await cookies() const cookieData = cookieStore.get(CS_COOKIE_NAME)?.value diff --git a/packages/nextjs/src/logger/index.ts b/packages/nextjs/src/logger/index.ts index 77c73e92..864dae1c 100644 --- a/packages/nextjs/src/logger/index.ts +++ b/packages/nextjs/src/logger/index.ts @@ -1,14 +1,39 @@ -import { debuglog } from 'node:util' -type LoggerFunction = (...args: unknown[]) => void +function getLevelValue(level: string): number { + switch (level) { + case 'debug': + return 10 + case 'info': + return 20 + case 'error': + return 30 + default: + return 20 // default to 'info' + } +} + +const envLogLevel = process.env.JSEQL_LOG_LEVEL || 'info' +const currentLevel = getLevelValue(envLogLevel) + +function debug(...args: unknown[]): void { + if (currentLevel <= getLevelValue('debug')) { + console.debug('[DEBUG]', ...args) + } +} + +function info(...args: unknown[]): void { + if (currentLevel <= getLevelValue('info')) { + console.info('[INFO]', ...args) + } +} -const log = { - debug: debuglog('jseql-debug') as LoggerFunction, - error: debuglog('jseql-error') as LoggerFunction, - info: debuglog('jseql-info') as LoggerFunction, +function error(...args: unknown[]): void { + if (currentLevel <= getLevelValue('error')) { + console.error('[ERROR]', ...args) + } } export const logger = { - debug: (...args: unknown[]) => log.debug(...args), - error: (...args: unknown[]) => log.error(...args), - info: (...args: unknown[]) => log.info(...args), + debug, + info, + error, } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 961ae145..e96965e5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -128,9 +128,6 @@ importers: specifier: 4.24.0 version: 4.24.0 devDependencies: - '@cipherstash/jseql': - specifier: workspace:* - version: link:../jseql '@clerk/nextjs': specifier: ^6.9.7 version: 6.9.7(next@15.1.4(react-dom@19.0.0(react@19.0.0))(react@19.0.0))(react-dom@19.0.0(react@19.0.0))(react@19.0.0)