Nikita Fedyashev
Nikita Fedyashev

Reputation: 19003

Log threads in PHP

Recently I've faced such problem. I'm working on the application, in which logging is very important.

By logs in that application I mean the messages, like

(1) and (3) are messages of one particular request/command - so when I want to see the logs, I need to get all the information about this request log thread.

Because of the application is highly loaded and uses AJAX a lot, log message of different users/operations/requests can be mixed.

Temporary, I've fixed this problem like this:

I've created at the beginning of the request some unique UUID code, and prepend any log messages with this unique code. So, I can find all the messages of particular thread by simple grep UNIX command.

That solves the problem, but I'm not sure it is the best solution for the task. Looks more like reinventing the wheel. What solutions would you reccomend for this problem?

Upvotes: 0

Views: 575

Answers (3)

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

If you need to view all logs for a particular user session, use

session_start()

at the top of your script(s) and then prepend

session_id()

to the front of each log message.

Upvotes: 1

Steve Weet
Steve Weet

Reputation: 28402

The de-facto standard for logging in the Java world for many years has been log4j. This has been ported to a number of other languages log4Net, log4Ruby and log4Php

I have never used log4php but if it implements the same patterns/algorithms as the other frameworks it will certainly do your job.

I will not claim that it's better than either of the options recommended by karim79 just that it follows a proven design.

Upvotes: 0

karim79
karim79

Reputation: 342655

Off the top of my head, a couple of suggestions (if you haven't already implemented them)

  1. Log each different type of event into a separate log file, e.g. BookPurchases.log, InvoiceApprovals.log. That said keep a raw output log of everything in case it's the sequence of events that matters as opposed to the classification.

  2. Use a logging package if you don't already have one, examples are The Log Package, or Zend_Log where you can have different logging priorities.

I don't know if that's of any use to you, as what you have said seems pretty solid.

Upvotes: 1

Related Questions