From 77b145d0a9a9f48238337c69bf58f3d3b7a5b1d7 Mon Sep 17 00:00:00 2001 From: Jonathan Perichon Date: Mon, 4 Apr 2016 10:42:16 -0700 Subject: [PATCH] Allow each container to log to a different log set --- README.md | 10 ++++++++++ index.js | 21 +++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cd16fa9..62fecc6 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,16 @@ forwarded with: * `--skipByName REGEXP`: do not forward logs/stats for the containers whose name matches the given REGEXP. * `--skipByImage REGEXP`: do not forward logs/stats for the containers whose image matches the given REGEXP. +You can pass the `--containersTokensFilepath` flag if you want different containers logs +to be published to different log sets. +The configuration file needs to respect the following format: +```json +{ + "repoName1": "logentries_token", + "repoName2": "logentries_token" +} +``` + ### Running container in a restricted environment. Some environments(such as Google Compute Engine) does not allow to access the docker socket without special privileges. You will get EACCES(`Error: read EACCES`) error if you try to run the container. To run the container in such environments add --privileged to the `docker run` command. diff --git a/index.js b/index.js index 1853784..020e960 100755 --- a/index.js +++ b/index.js @@ -29,7 +29,6 @@ function connect(opts) { return stream; } - function start(opts) { var logsToken = opts.logstoken || opts.token; var statsToken = opts.statstoken || opts.token; @@ -37,11 +36,24 @@ function start(opts) { var out; var noRestart = function() {}; + var containersTokens = null; + if (opts.containersTokensFilepath) { + containersTokens = require(opts.containersTokensFilepath); + } + var filter = through.obj(function(obj, enc, cb) { addAll(opts.add, obj); var token = ''; - if (obj.line) { + if (obj.line && obj.image && containersTokens) { + var regex = /(.+):\w+/g; + var match = regex.exec(obj.image); + + if (match != null) { + var repoName = match[1]; + token = containersTokens[repoName]; + } + } else if (obj.line) { token = logsToken; } else if (obj.type) { @@ -69,7 +81,7 @@ function start(opts) { opts.events = events; - if (opts.logs !== false && logsToken) { + if (opts.logs !== false && (logsToken || containersTokens)) { loghose = logFactory(opts); loghose.pipe(filter); streamsOpened++; @@ -175,7 +187,7 @@ function cli() { } }); - if (argv.help || !(argv.token || argv.logstoken || argv.statstoken || argv.eventstoken)) { + if (argv.help || !(argv.token || argv.logstoken || argv.statstoken || argv.eventstoken || argv.containersTokensFilepath)) { console.log('Usage: docker-logentries [-l LOGSTOKEN] [-k STATSTOKEN] [-e EVENTSTOKEN]\n' + ' [-t TOKEN] [--secure] [--json]\n' + ' [--no-newline] [--no-stats] [--no-logs] [--no-dockerEvents]\n' + @@ -183,6 +195,7 @@ function cli() { ' [--matchByImage REGEXP] [--matchByName REGEXP]\n' + ' [--skipByImage REGEXP] [--skipByName REGEXP]\n' + ' [--server HOSTNAME] [--port PORT]\n' + + ' [--containersTokensFilepath CONTAINERSTOKENFILEPATH]\n' + ' [--help]'); process.exit(1);