Skip to content

Example: AWS CloudWatch Logs

Denis Bardadym edited this page Jan 30, 2016 · 2 revisions

2. Add example config to /var/awslogs/etc/config/ like:

[/home/ubuntu/api/log/debug.log]
file = /home/ubuntu/api/log/debug.log
log_group_name = api-0.1.0-debug
log_stream_name = {instance_id}
datetime_format = %Y-%m-%d %H:%M:%S,%f

3. Create such config in your project:

'use strict';

const fs = require('fs');
const path = require('path');
const huzzah = require('huzzah');
const handlers = require('huzzah/handlers');

const jsonFormat = require('huzzah/json-format');
const strftimeCompile = require('huzzah/utils/strftime');

const config = require('./config');

const CONSOLE_FORMAT = '%date %highlight(%-5level) %cyan(%logger) - %message%n%error';

let rootSettings = huzzah.settings('root');

rootSettings
  .setLevel(config.get('log.level'));

if(config.isDev || config.get('log.console', false)) {
  rootSettings.addHandler(new handlers.ConsoleHandler().setFormat(CONSOLE_FORMAT));
}

if(!config.isDev) {
  rootSettings.addHandler(
    new handlers.StreamHandler()
      .setFormat(jsonFormat({
        timestamp: strftimeCompile('%Y/%m/%d %H:%M:%S,%L000')
      }))
      .setStream(fs.createWriteStream(path.join(process.cwd(), 'log', 'debug.log'), { flags: 'a' }))
  );
}

module.exports = huzzah;

And you will have readable messages in console and json at the server