Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share a logfile between multiple services (each service with multiple threads), how to?

I have this problem that I need to solve for one of my projects. I need to create ONE log file for 3 different services (don't ask why, my boss requested it like this). Each service can have multiple threads trying to log info into the file, so my question is, what's the best way to do this?

Should I use a global mutex? Something like this:

procedure LogToFile(fn, str: string);
var
  F: TextFile;
begin
  logMutex.Acquire;
  try
{$I+}
    try
      AssignFile(F, fn);
      if FileExists(fn) then
        Append(F)
      else
        Rewrite(F);
      Writeln(F, DateTimeToStr(Now) + ': ' + str);
      CloseFile(F);
    except
    end;
{$I-}
  finally
      logMutex.Release;
  end;
end;

initialization
  logMutex := SyncObjs.TMutex.Create(nil, False,'some_global_mutex');

finalization
  logMutex.Free;

Any better ideas?

Edit: Should I build another service, a logger service, that waits for messages that need to be logged from the other services and then only one service has to deal with the log files? If this is a good solution, what's the best way to communicate between services? I could use Indy...

like image 613
avraam aaron Avatar asked Nov 15 '12 19:11

avraam aaron


2 Answers

Your solution using a named mutex will work and is for sure the simplest approach.

like image 170
David Heffernan Avatar answered Nov 15 '22 07:11

David Heffernan


In a big project with around 50-80 applications running on multiple Terminal Servers (which makes it hard to synchronize), I managed to collect all log output to single central file.

The apps write the log output to a application server over UDP, using the open source Log4D framework and Internet Direct (Indy). The application server has a UDP server app running which receives the log messages and writes them to a single file.

As the log messages include meta information (logger name etc.), the logging server still can be configured to write separate files per application, in addition to the main file. For example, some log messages sent from the clients contain database performance data, they will be written to a separate log file to have them ready for analysis without further filtering steps.

like image 34
mjn Avatar answered Nov 15 '22 07:11

mjn