# ctxobj

> Objects with context, especially good for contextual logging

Latest version **0.3.0** (published 2012-10-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install ctxobj
pnpm add ctxobj
yarn add ctxobj
bun add ctxobj
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2012-10-02 |
| First published | 2012-01-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | 0.8.x |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Elad Ben-Israel |
| Maintainers | eladb, yosefd |
| Keywords | context, logging, console, log |

## Links

- npm: https://www.npmjs.com/package/ctxobj
- Repository: http://github.com/anodejs/node-ctxobj
- npm.io page: https://npm.io/package/ctxobj

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 0.3.0 (latest) — 2012-10-02
- 0.2.1 — 2012-07-15
- 0.2.0 — 2012-01-04
- 0.1.0 — 2012-01-03

## README

# ctxobj #

[![Build Status](https://secure.travis-ci.org/anodejs/node-ctxobj.png?branch=master)](http://travis-ci.org/anodejs/node-ctxobj)

Adds context to objects. Good for contextual logging.

## ctxobj.new(obj) ##

Decorates `obj` with a context. `pushctx` or `popctx` will extende context.

```nodejs
var ctxobj = require('ctxobj');

// just an object
var obj0 = { x: 1, y: { val: 2 } };

// decorate with context
var obj1 = ctxobj.new(obj0);

// push and pop data to the context
obj1.pushctx('c1')
obj1.pushctx('c2a').pushctx('c2b')
obj1.popctx().popctx();

// enumarate contexts
obj1.forEachCtx(function(c) {
  console.log(c);
});
```

Output:

```
c1
```

## ctxobj.console(console) ##

Decorates console-like objects with a context that is prepended to each `console.xxx()` call.

```node.js
var ctxobj = require('ctxobj');

var log0 = ctxobj.console(console);
log0.info('no context yet');

var i = 0;
var log1 = log0.pushctx('c1').pushctx(function() { return i++; });
log1.warn('now with a two items in the context');
log1.info('yo yo yo');

var log2 = log1.popctx();
log2.error('only one item in the context now');
```

Output:

```
no context yet
[c1][0] now with a two items in the context
[c1][1] yo yo yo
[c1] only one item in the context now
```

Some sugar:

### ctxobj.timestamp() ###
Pushes a timestamp function to the context

### ctxobj.stacktop(skip) ###
Pushes file name and line number of log line to the context (skipping `skip` frames).

## Licence ##

MIT

---
_Source: https://npm.io/package/ctxobj · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
