Skip to content
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
6 changes: 6 additions & 0 deletions .changeset/pink-needles-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@cipherstash/nextjs": minor
"@cipherstash/jseql": minor
---

Implement packageless logging framework.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 34 additions & 9 deletions packages/jseql/src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -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,
}
1 change: 0 additions & 1 deletion packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"dev": "tsup --watch"
},
"devDependencies": {
"@cipherstash/jseql": "workspace:*",
"@clerk/nextjs": "^6.9.7",
"dotenv": "^16.4.7",
"tsup": "^8.3.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/nextjs/src/clerk/index.ts
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
6 changes: 5 additions & 1 deletion packages/nextjs/src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
43 changes: 34 additions & 9 deletions packages/nextjs/src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -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,
}
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.