caprese

Capped Log for Node.js. A compact library with no dependencies.

Usage no npm install needed!

<script type="module">
  import caprese from 'https://cdn.skypack.dev/caprese';
</script>

README

caprese NPM version Build Status

Capped Log for Node.js. A compact library with no dependencies.

Capped logs are fixed-size collections of messages that work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new messages by overwriting the oldest messages in the collection.

Installation

npm install caprese

Quick Example

var Caprese = require('caprese'); 

var config = {size: 1024};
var cap = new Caprese('./log.cap', config);
cap.add(Caprese.INFO, 'My log message.');

Documentation

Caprese

Query

## Configuration
var config = {
    overwrite: false,
    resident: true,
    size: 1024
}
  • overwrite - Create new capped log file even if one already exist.
  • resident - Create new capped log in memory. All data are lost on process exit.
  • size - Size of capped log file in bytes. Optional. Default value is 1MB. Minimum value is 15 bytes (for one empty message). Maximum value is 4GB.
## Caprese

Open a capped log. If file doesn't exist, new capped log is created.

Arguments

  • file - Path to capped log file. Optional.
  • config - Config. Optional.
  • callback(err) - A callback which is called after capped log has loaded, or an error has occurred. Optional.

Example

var cap = new Caprese()                                           // create 1MB resident capped log
var cap = new Caprese('./file.cap')                               // create 1MB capped log
var cap = new Caprese('./file.cap', {size: 1024})                 // create 1KB capped log
var cap = new Caprese('./file.cap', {size: 1024}, function() {})  // create 1KB capped log and call a callback function
var cap = new Caprese({size: 1024})                               // create 1KB resident capped log
var cap = new Caprese({size: 1024}, function() {})                // create 1KB resident capped log and call a callback function
var cap = new Caprese(function() {})                              // create 1MB resident capped log and call a callback function
var cap = new Caprese('./file.cap', {size: 1024, resident: true}) // create 1KB resident capped log

### add(type, message, [callback])

Add message to capped log.

Arguments

  • type - Message type (1 - info, 2 - warning, 3 - error)
  • message - Message. Max 65535 bytes in utf8 encoding.
  • callback(err) - A callback which is called after message was saved, or an error has occurred. Optional.

Example

cap.add(Caprese.ERROR, 'My error message.', function(err) {
    // ...
});

### close()

Close capped log.

Arguments

  • callback(err) - A callback which is called after log has closed, or an error has occurred. Optional.

Example

cap.close(function(err) {
    // ...
});

### count()

Return number of messages in capped log.

Example

console.log(cap.count());

### select()

Return Query object to query messages from capped log.

Example

var query = cap.select();
## Query

Query messages from capped log. Each method return query, so calls can be chained.

Example

cap.select().top(1).desc().go(function(err, results) {
    // ...
});

### asc()

Tell query to return messages in ascending order.

Example

cap.select().asc();

### desc()

Tell query to return messages in descending order.

Example

cap.select().desc();

### go() / toArray()

Return messages.

Example

cap.select().go(function(err, results) {
    // ...
});

### top() / limit()

Tell query to return only first X messages.

Arguments

  • limit - Limit of messages to return.

Example

cap.select().top(limit);

### where() / filter()

Tell query to filter messages.

Arguments

  • condition - Key-value collection. Only type filter is available atm.

Example

cap.select().where({type: Caprese.ERROR});
## License

Copyright (c) 2014 Patrik Simek

The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.