Appends text or JSON to files on S3.
npm install s3-append
Appending isn't magic: as files get larger the initial read and any flushes will take longer to run. However, appending is still smart and allows synchronous calls for quick logging. Writing the file only happens when .flush()
is called. There is no inherent concurrency support, but see this section for how you can handle this.
Create an S3Config
object to authenticate with AWS:
var S3Config = require('s3-append').S3Config;
var config = new S3Config({
"accessKeyId": "<YOUR DATA>",
"secretAccessKey": "<YOUR DATA>",
"region": "<YOUR DATA>",
"bucket": "<YOUR DATA>"
});
// Using config from above
var S3Append = require('s3-append').S3Append;
var service = new S3Append(config, 'folder/log.txt', Format.Text);
service.append("This is simple logging");
service.appendWithDate("This is logging that prefixes the line with a sortable date string");
service.append("This is logging that allows %s", ['replacements']);
service.flush()
.then(function() {
console.log("Done!");
})
.catch(function(err) {
console.error(err.message);
});
Your resulting file would look like this
This is simple logging
2016-03-10 11:21:30.083: This is logging that prefixes the line with a sortable date string
This is logging that allows replacements
Alternatively, you can append JSON objects and they'll be stored as an array.
// Using config from above
var S3Append = require('s3-append').S3Append;
var service = new S3Append(config, 'folder/log.txt', Format.Json);
service.append({ a: 1, b: "Hi" });
service.append({ a: 2, b: "Goodbye" });
service.flush()
.then(function() {
console.log("Done!");
})
.catch(function(err) {
console.error(err.message);
});
Your resulting file would look like this
[{"a":1,"b":"Hi"},{"a":2,"b":"Goodbye"}]
You can easily consolidate many files into one and apply sorting to the final file. This is a great way to get passed the concurrency limitations: each parallel process should log to its own file and then they can get merged together.
// Using config from above
var S3Consolidator = require('s3-append').S3Consolidator;
var service = new S3Consolidator(config);
service.consolidate(['logs1.txt', 'logs2.txt'], 'logs.txt')
.then(function() {
console.log("Done!");
})
.catch(function(err) {
console.error(err.message);
});
After running this code, logs1.txt
and logs2.txt
will no longer exist and logs.txt
will contain text from both files.
var Format = require('s3-append').Format;
- Text - Writes plain text
- Json
String
- Appends objects into an array
var S3Append = require('s3-append').S3Append;
A service to append text to S3 files.
- config
S3Config
- Configuration details - key
String
- Where to write the file to on S3. - format
Format, optional
- How to write the data, defaults toFormat.Text
- acl
String, optional
- The permissions to assign to the file on S3, defaults toprivate
Appends text or JSON to the file. The returned promise can be ignored unless flushing.
- text
String|Object
- The object or text to append. - formatArgs
Array, optional
- An array of objects to format the text with. - autoFlush
Boolean, optional
- True to force writing to S3.
Only for text appending, automatically prefixes the line with a sortable date. The returned promise can be ignored unless flushing.
- text
String|Object
- The object or text to append. - formatArgs
Array, optional
- An array of objects to format the text with. - autoFlush
Boolean, optional
- True to force writing to S3.
Waits for all contents to be written.
Returns the up-to-date text or JSON contents of the file.
Permanently deletes the log file.
var S3Consolidator = require('s3-append').S3Consolidator;
A service to consolidate separate log files into one.
- config
S3Config
- Configuration details
Reads and stitches together the contents of these keys. By default text lines are sorted alphabetically and JSON arrays are sorted by date if the object has one of these keys: 'created', 'createDate', 'creationDate', 'date'
- keys
String[]
- A list of keys on S3. - sorter
(FileContents[]) => string|Promise, optional
- A custom way to sort files.
You'll get back an object like this:
- format
Format
- The deciphered format of the data. - contents
string
- The full contents of the logs.
Reads and stitches together the contents of these keys and then writes them to the consolidatedKey
. Performs the same sorting as concatonate
. All keys except for consolidatedKey
will be deleted.
- keys
String[]
- A list of keys on S3. - consolidatedKey
String
- The key to write the consolidated data to. - sorter
(FileContents[]) => string|Promise, optional
- A custom way to sort files. - acl
String, optional
- The permissions to assign to the file on S3, defaults toprivate