Skip to content

Commit

Permalink
feat: support cloudflare module workers
Browse files Browse the repository at this point in the history
BREAKING: one must `enable()` and not assume an ambiant environment variable
  • Loading branch information
maraisr committed Nov 4, 2021
1 parent 9ea2cc8 commit 61486c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
23 changes: 21 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,28 @@ DEBUG=scopeA:two,scopeB:* node example.js

#### _workers_

Create an [Environment Variable](https://developers.cloudflare.com/workers/platform/environments) with `DEBUG`.
As of version v0.3.0 to enable log events you must use the `enable` programmatic api, this is due to Module Workers no
longer offering global environment variables, and instead they are injected through an api.

> ⚠️ Specifically referencing the Cloudflare Workers
Ambiant logs do however need to be statically enabled. (put a `enable()` in module scope).

<details><summary>Example</summary>

```ts
import { diary, enable } from 'diary';

const logger = diary('my-worker');

export default {
async fetch(req, env, context) {
enable(env.DEBUG);

logger.info('request for', req.url);
},
};
```

</details>

#### _programmatic_

Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ export const enable = (allows_query: string) => {
};

// read `localstorage`/`env` for scope "name"s allowed to log
enable(
(__TARGET__ === 'node'
? process.env.DEBUG
: __TARGET__ === 'browser'
? localStorage.getItem('DEBUG')
: __TARGET__ === 'worker'
? DEBUG
: null
) || 'a^',
);
if (__TARGET__ !== 'worker') {
enable(
(__TARGET__ === 'node'
? process.env.DEBUG
: __TARGET__ === 'browser'
? localStorage.getItem('DEBUG')
: null
) || 'a^',
);
}

// ~ Logger

Expand Down

0 comments on commit 61486c8

Please sign in to comment.