<p>This answer is more about scalability considerations - if the number of workers can be high and/or multiple of them can produce logs at high rate at the same time.</p>

<p>Yes, using multiple logfiles simultaneously is a good practice.</p>

<p>Attempting to combine into a single logfile logs from multiple workers <strong>in real time</strong> will raise problems:</p>

<ul>
<li>using blocking mechanims to prevent message loss will slow down the workers</li>
<li>log messages can appear out-of-order in the combined logfile</li>
<li>a centralized logging facility which combines the logs can be overloaded due to limited write speed, messages would be lost </li>
</ul>

<p>Sharding logfiles (using multiple logfiles active in the same time) is itself a technique used by some hosting providers offering high performance, scalable centralized logging services. For example, when exporting logs to files Google's <a href="https://cloud.google.com/logging/" rel="noreferrer">StackDriver Logging</a> produces multiple sharded logfiles. From <a href="https://cloud.google.com/logging/docs/export/using_exported_logs#log_entries_in_google_cloud_storage" rel="noreferrer">Log entries in Google Cloud Storage</a>:</p>

<blockquote>
  <p>When you <a href="https://cloud.google.com/logging/docs/export/configure_export" rel="noreferrer">export logs</a> to a Cloud Storage bucket, Stackdriver
  Logging writes a set of files to the bucket. The files are organized
  in directory hierarchies by log type and date. The log type can be a
  simple name like <code>syslog</code> or a compound name like
  <code>appengine.googleapis.com/request_log</code>. If these logs were stored in a
  bucket named <code>my-gcs-bucket</code>, then the directories would be named as
  in the following example:</p>

<pre><code>my-gcs-bucket/syslog/YYYY/MM/DD/
my-gcs-bucket/appengine.googleapis.com/request_log/YYYY/MM/DD/
</code></pre>
  
  <p>A single bucket can contain logs from multiple log types.</p>
  
  <p>The leaf directories (<code>DD/</code>) contain multiple files, each of which
  holds the exported log entries for a time period specified in the file
  name. The files are sharded and their names end in a shard number,
  <code>Sn</code> or <code>An</code> (n=0, 1, 2, ...). For example, here are two files that
  might be stored within the <code>directory
  my-gcs-bucket/syslog/2015/01/13/</code>:</p>

<pre><code>08:00:00_08:59:59_S0.json
08:00:00_08:59:59_S1.json
</code></pre>
  
  <p>These two files together contain the <code>syslog</code> log entries for all
  instances during the hour beginning 0800 UTC. To get all the log
  entries, you must read all the shards for each time period—in this
  case, file shards 0 and 1. The number of file shards written can
  change for every time period depending on the volume of log entries.</p>
</blockquote>

<p>Such high-performance logging services can also offer alternatives to logging to files, management of logfiles can thus be avoided altogether if that is of interest:</p>

<ul>
<li>inserting logs directly into a database. For example Stackdriver Logging can <a href="https://cloud.google.com/logging/docs/export/using_exported_logs#log_entries_in_google_bigquery" rel="noreferrer">push logs directly into Google BigQuery</a></li>
<li>pushing logs directly into a processing engine. For example Stackdriver Logging can <a href="https://cloud.google.com/logging/docs/export/using_exported_logs#log_entries_in_google_pubsub_topics" rel="noreferrer">push logs into Google Pub/Sub topics</a></li>
</ul>

<p>Finally - if real-time logfile merging is not a requirement having multiple logfiles can help with offline log management:</p>

<ul>
<li>easy to devise progressive log backup, compression, archiving and eventual disposal schemes</li>
<li>parallel processing of multiple sets of logs (logfiles) is possible, reducing/avoiding bottleneck effects</li>
<li>no file splitting and re-writing necessary</li>
</ul>

CC BY-SA 3.0
2017-03-07T16:27:33.763Z
2017-03-07T16:27:33.763Z
Dan Cornilescu
<p>"Serverless" mostly just means you've got relatively simple microservices, generally just a little webapp or a single function that is automatically connected to a REST frontend. The same concepts apply as you would use for a more traditional web services: usually some mix of remote syslog and ElasticSearch writers.</p>

<p>Networked or remote syslog has been around for a long time and has a fairly robust set of tools around it. You would have to run the central syslog server(s) but the protocol is very simple and there are pure client libraries in every language that you can use for sending logs. One common problem with remote syslog is that it has traditionally been based around UDP. This means that under heavy load, some log messages may be lost. This could be a good thing, helping avoid a cascade overload, but it is something to be aware of. Some newer syslog daemons also support a TCP-based protocol, but client support is less unified so just do your research.</p>

<p>More recent but very popular is logging to ElasticSearch. This is mostly useful because of the Kibana dashboard and Logstash tooklit (often called ELK, ElasticSearch+Logstash+Kibana). Amazon even offers a hosted ElasticSearch option, making it somewhat easier to get started. ES uses a relatively simple REST API, so any language with an HTTP client (read: all of them) should be okay with logging to ES but make sure you are careful with blocking network operations in cases of partial system outages (i.e. make sure your app won't get stuck in a logging call that will never succeed and stop servicing user requests).</p>

<p>More complex logging topologies are bounded only by your imagination, though these days you'll see a lot of use of the Kafka database/queue/whatever-you-want-to-call-it as a nexus point in very complex log distribution systems.</p>

<p>On the "serverless" side, you'll generally want to integrate with these systems directly at the network level, so sending log data directly to syslog or ES from your service/function, rather than writing to local files (though maybe echo to those too for local debugging and development).</p>

CC BY-SA 3.0
2017-03-07T08:47:54.997Z
2017-03-07T08:47:54.997Z
coderanger
<p>I have the following setting:</p>

<blockquote>
  <p>Create multiple workers, do a computation  and terminate them after the
  computation is done.</p>
</blockquote>

<p>So, every-time it’ll be a different instance running the task, so each host will have its own a log file, this will result in a huge list of files.  </p>

<p>Is it a good practice? If not, what would be a better way for logging the task processing in this particular use-case?</p>

<p>PS: My infrastructure is serverless. So, for now, I am logging to (AWS)CloudWatch. But, please answer the question independently of AWS, and suiting a serverless setup as much as possible.</p>

<logging><architecture><serverless>
What is a good logging practice for distributed tasks?
Dawny33
# What is a good logging practice for distributed tasks?

(Asked by: username_0 on 2017-03-07)

I have the following setting:

> Create multiple workers, do a computation and terminate them after the
> computation is done.

So, every-time it’ll be a different instance running the task, so each host will have its own a log file, this will result in a huge list of files.

Is it a good practice? If not, what would be a better way for logging the task processing in this particular use-case?

PS: My infrastructure is serverless. So, for now, I am logging to (AWS)CloudWatch. But, please answer the question independently of AWS, and suiting a serverless setup as much as possible.

**Tags:** logging, architecture, serverless


---

## 2 answers

### Answer by username_2 on 2017-03-07. Score: 12 | Accepted answer

"Serverless" mostly just means you've got relatively simple microservices, generally just a little webapp or a single function that is automatically connected to a REST frontend. The same concepts apply as you would use for a more traditional web services: usually some mix of remote syslog and ElasticSearch writers.

Networked or remote syslog has been around for a long time and has a fairly robust set of tools around it. You would have to run the central syslog server(s) but the protocol is very simple and there are pure client libraries in every language that you can use for sending logs. One common problem with remote syslog is that it has traditionally been based around UDP. This means that under heavy load, some log messages may be lost. This could be a good thing, helping avoid a cascade overload, but it is something to be aware of. Some newer syslog daemons also support a TCP-based protocol, but client support is less unified so just do your research.

More recent but very popular is logging to ElasticSearch. This is mostly useful because of the Kibana dashboard and Logstash tooklit (often called ELK, ElasticSearch+Logstash+Kibana). Amazon even offers a hosted ElasticSearch option, making it somewhat easier to get started. ES uses a relatively simple REST API, so any language with an HTTP client (read: all of them) should be okay with logging to ES but make sure you are careful with blocking network operations in cases of partial system outages (i.e. make sure your app won't get stuck in a logging call that will never succeed and stop servicing user requests).

More complex logging topologies are bounded only by your imagination, though these days you'll see a lot of use of the Kafka database/queue/whatever-you-want-to-call-it as a nexus point in very complex log distribution systems.

On the "serverless" side, you'll generally want to integrate with these systems directly at the network level, so sending log data directly to syslog or ES from your service/function, rather than writing to local files (though maybe echo to those too for local debugging and development).


---

### Answer by username_1 on 2017-03-07. Score: 6

This answer is more about scalability considerations - if the number of workers can be high and/or multiple of them can produce logs at high rate at the same time.
Yes, using multiple logfiles simultaneously is a good practice.
Attempting to combine into a single logfile logs from multiple workers in real time will raise problems:

using blocking mechanims to prevent message loss will slow down the workers
log messages can appear out-of-order in the combined logfile
a centralized logging facility which combines the logs can be overloaded due to limited write speed, messages would be lost 

Sharding logfiles (using multiple logfiles active in the same time) is itself a technique used by some hosting providers offering high performance, scalable centralized logging services. For example, when exporting logs to files Google's StackDriver Logging produces multiple sharded logfiles. From Log entries in Google Cloud Storage:

When you export logs to a Cloud Storage bucket, Stackdriver
  Logging writes a set of files to the bucket. The files are organized
  in directory hierarchies by log type and date. The log type can be a
  simple name like syslog or a compound name like
  appengine.googleapis.com/request_log. If these logs were stored in a
  bucket named my-gcs-bucket, then the directories would be named as
  in the following example:
my-gcs-bucket/syslog/YYYY/MM/DD/
my-gcs-bucket/appengine.googleapis.com/request_log/YYYY/MM/DD/

A single bucket can contain logs from multiple log types.
The leaf directories (DD/) contain multiple files, each of which
  holds the exported log entries for a time period specified in the file
  name. The files are sharded and their names end in a shard number,
  Sn or An (n=0, 1, 2, ...). For example, here are two files that
  might be stored within the directory
  my-gcs-bucket/syslog/2015/01/13/:
08:00:00_08:59:59_S0.json
08:00:00_08:59:59_S1.json

These two files together contain the syslog log entries for all
  instances during the hour beginning 0800 UTC. To get all the log
  entries, you must read all the shards for each time period—in this
  case, file shards 0 and 1. The number of file shards written can
  change for every time period depending on the volume of log entries.

Such high-performance logging services can also offer alternatives to logging to files, management of logfiles can thus be avoided altogether if that is of interest:

inserting logs directly into a database. For example Stackdriver Logging can push logs directly into Google BigQuery
pushing logs directly into a processing engine. For example Stackdriver Logging can push logs into Google Pub/Sub topics

Finally - if real-time logfile merging is not a requirement having multiple logfiles can help with offline log management:

easy to devise progressive log backup, compression, archiving and eventual disposal schemes
parallel processing of multiple sets of logs (logfiles) is possible, reducing/avoiding bottleneck effects
no file splitting and re-writing necessary


---

