Skip to content

Commit

Permalink
Merge pull request #36 from cipherstash/logging
Browse files Browse the repository at this point in the history
Logging
  • Loading branch information
calvinbrewer authored Jan 17, 2025
2 parents 151f786 + c0123be commit c9cfaab
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 62 deletions.
6 changes: 6 additions & 0 deletions .changeset/forty-flowers-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@cipherstash/nextjs": minor
"@cipherstash/jseql": minor
---

Replaced logtape with native node debuglog.
29 changes: 8 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,32 +419,19 @@ You can read more about this feature and implementation [here](https://github.co

## Logging

> [!WARNING]
> [!IMPORTANT]
> `jseql` will NEVER log plaintext data.
> This is by design to prevent sensitive data from being logged.
`jseql` uses [logtape](https://github.com/logtape/logtape) for logging, which allows you to control the logging output and integrate it with your application's logging system.

To set up logging for `jseql`, you need to configure a sink for the `'jseql'` logger.
`@cipherstash/jseql` and `@cipherstash/nextjs` use [node's built-in debug logging](https://nodejs.org/api/util.html#utildebuglogsection-callback) for logging.

**Setup Example:**
By default, the logger is disabled, but you can enable it by configuring the following environment variables:

```typescript
// Configure the logger
import { configure, getConsoleSink, getFileSink } from '@logtape/logtape'

await configure({
sinks: {
console: getConsoleSink(),
},
loggers: [
{
category: ['jseql'],
level: 'debug',
sinks: ['console'],
},
],
})
```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
```

## Examples
Expand Down
15 changes: 0 additions & 15 deletions packages/jseql/__tests__/jseql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ import { describe, expect, it } from 'vitest'
import { createEqlPayload, getPlaintext, eql, LockContext } from '../src'
import type { CsPlaintextV1Schema } from '../src/cs_plaintext_v1'

import { configure, getConsoleSink } from '@logtape/logtape'

await configure({
sinks: {
console: getConsoleSink(),
},
loggers: [
{
category: ['jseql'],
level: 'info',
sinks: ['console'],
},
],
})

describe('createEqlPayload', () => {
it('should create a payload with the correct default values', () => {
const result = createEqlPayload({
Expand Down
3 changes: 1 addition & 2 deletions packages/jseql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
"vitest": "^2.1.8"
},
"peerDependencies": {
"typescript": "^5.0.0",
"@logtape/logtape": "^0"
"typescript": "^5.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/jseql/src/eql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ const {
encryptBulk,
decryptBulk,
} = require('@cipherstash/jseql-ffi')
import { getLogger } from '@logtape/logtape'
const logger = getLogger(['jseql'])
import { logger } from '../logger'
import type { LockContext } from '../identify'

export class EqlClient {
Expand Down
3 changes: 1 addition & 2 deletions packages/jseql/src/identify/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getLogger } from '@logtape/logtape'
const logger = getLogger(['jseql'])
import { logger } from '../logger'

export type CtsRegions = 'ap-southeast-2'

Expand Down
4 changes: 1 addition & 3 deletions packages/jseql/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getLogger } from '@logtape/logtape'

import type {
CsPlaintextV1Schema,
ForQuery,
Expand All @@ -10,7 +8,7 @@ import type {
Plaintext,
} from './cs_plaintext_v1'
import { EqlClient } from './eql'
const logger = getLogger(['jseql'])
import { logger } from './logger'

export const eql = (): Promise<EqlClient> => {
const client = new EqlClient()
Expand Down
15 changes: 15 additions & 0 deletions packages/jseql/src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const util = require('node:util')
const log = {
debug: util.debuglog('jseql-debug'),
error: util.debuglog('jseql-error'),
info: util.debuglog('jseql-info'),
}

export const logger = {
// biome-ignore lint/suspicious/noExplicitAny: jseql-debug is not typed
debug: (...args: any[]) => log.debug(...args),
// biome-ignore lint/suspicious/noExplicitAny: jseql-error is not typed
error: (...args: any[]) => log.error(...args),
// biome-ignore lint/suspicious/noExplicitAny: jseql-info is not typed
info: (...args: any[]) => log.info(...args),
}
1 change: 0 additions & 1 deletion packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"vitest": "^2.1.8"
},
"peerDependencies": {
"@logtape/logtape": "^0",
"next": "^14 || ^15",
"typescript": "^5.0.0"
},
Expand Down
4 changes: 1 addition & 3 deletions packages/nextjs/src/clerk/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { ClerkMiddlewareAuth } from '@clerk/nextjs/server'
import { getLogger } from '@logtape/logtape'
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
import { CS_COOKIE_NAME } from '../index'
import type { CtsToken } from '@cipherstash/jseql'

const logger = getLogger(['jseql'])
import { logger } from '../logger'

export const jseqlClerkMiddleware = async (
auth: ClerkMiddlewareAuth,
Expand Down
3 changes: 1 addition & 2 deletions packages/nextjs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { cookies } from 'next/headers'
import { getLogger } from '@logtape/logtape'
import type { CtsToken } from '@cipherstash/jseql'
import { logger } from './logger'

const logger = getLogger(['jseql'])
export const CS_COOKIE_NAME = '__cipherstash_cts_session'

export const getCtsToken = async () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/nextjs/src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const util = require('node:util')
const log = {
debug: util.debuglog('jseql-debug'),
error: util.debuglog('jseql-error'),
info: util.debuglog('jseql-info'),
}

export const logger = {
// biome-ignore lint/suspicious/noExplicitAny: jseql-debug is not typed
debug: (...args: any[]) => log.debug(...args),
// biome-ignore lint/suspicious/noExplicitAny: jseql-error is not typed
error: (...args: any[]) => log.error(...args),
// biome-ignore lint/suspicious/noExplicitAny: jseql-info is not typed
info: (...args: any[]) => log.info(...args),
}
11 changes: 0 additions & 11 deletions pnpm-lock.yaml

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

0 comments on commit c9cfaab

Please sign in to comment.