PHP logging class v. 1.2

Hi guys,
I've decided to give out a PHP class I created to easily log anything we want in our websites.

Features

This are the things the logging class will do:

  • Store different security levels (Low, Medium, and High)
  • Get the real visitor's IP for every log entry
  • Allow several optional parameters, such as message, priority, page...
  • Easily translated into other languages
  • TABLE_LOGS.sql.txt - This file creates the table 'log'

And this is a screenshot of the log page in action (Or go to the sample page and take a look)
Screenshot of log

Class files and code:

The following files are the really important files:

  • log.class.php - This file contains the logging functions.
  • database.php - This file handles database connection and queries.
  • config.php - This file defines the configuration constants used for database queries. You should only edit this file.
  • logInit.php - This file includes all necessary files and initializes the classes storing them in the appropriate objects

Also, for the example, there are two other files: 'log.php' and 'test.php'. The log.php file shows a sample log, and the test.php shows a sample usage page.

Log in use:

I've set up a test page and a log page. In the test page there are several "common" stuff that you would normally log (A login form, an edit form, and a contact form)

I would recommend that you check those out, in any order you like, and experiment with the different options you'll see in the test page

[ad#blog]

Usage:

The usage of this logging class is VERY very simple, First of all download the zip with the needed files.
Then you must fill in all the variables in the config.php page.
Once all is configured, upload it to your server, and insert the following code in all the pages you will like to log activity:

  1.  
  2. < ?php
  3. //Initialize logging clases
  4. include('logInit.php');
  5. //
  6. ?>
  7.  

This is the initialization of the logging object and all other needed stuff.

The logging function is:

  1.  
  2. < ?php
  3. $log->logg('page','message','priority','class','mail');
  4. ?>
  5.  

Parameters explanation:

  • Page: This is the page where the log is placed. Set to 1 and it will be automatically detected.
  • Message: This is the message you want to have the log store. You can have some preset messages which are stored in the log.class.php file. Set to 1 to have a preset message stored.
  • Priority: This is the priority or Importance of the log entry. Values can be: High,Medium,Low.
  • Class: This is the class you want the log entry to have. Values are: Red, Danger, Yellow, Green, and Blue.
  • Mail: This setting can be yes or no. If it is not specified it will be set to "no".

You can edit the list of pre-set important pages in the file log.class.php, which is strongly recommended.

Download

Here you may download the zip file containing all necessary files to get the logging running:

Download zip - v. 1.2 - 22kb (3911 downloads)
Mirror #2 Zip file - @ PHP Classes (Without the icons)

It includes a css file with all needed styles, the icons, a log emptying page, a log sample page, the test page, and all the other files needed for the logging.

If you improve this script, or you find any bugs, report them using the comments please!
And remember, for any doubt or problem you have with the usage of this class, comment here and leave an email in the field email so I can answer you!

About Alex

Hello! I'm Alejandro U. Alvarez, an engineering student at Universidad de Oviedo (Spain). I started programming during my Freshman year in Brookline High School (Boston, MA), and then got more and more interested in web development. I'm almost in love with JavaScript, although I keep an open mind with other languages

28 thoughts on “PHP logging class v. 1.2

  1. Pingback: Blogsvine
  2. Well, I named the function logg because in php there is a core function called log(), and I just decided to add another g at the end to avoid duplicating… :)

  3. OK, now you just got me there! I didn’t know that methods inside classes could be named the same as core functions!
    Well, I guess I will leave this function as-is, but for future classes I’ll develop I’ll take that into account!

    Thanks for your comments Jesper, it is very nice to have someone bother :)

  4. Pingback: Web 2.0 Announcer
  5. Well, I thinks that a log system should not be based on a database if you want to log critical errors. For example, what do you do if there is a problem connecting the database ?

    In one on my site, I get a lot of too_many_connections errors. So logging into a database to trace such error is not the good solution…

    But if your purpose is to store not too critical information in order to display them to the admin user (failed connection) it could be enough.

    Thanks for sharing

  6. Thanks a lot for your comments!

    @fluminis:
    I guess that for that purpose it would be better to have it stored in a plain text file probably. But the aim of this logging system is more of a “activity” log than a parsing or database error. The main point of it is to store failed logins for example, to know if someone might be trying to hack a site through the forms, or the other things I have in the example page.

    Anyway it is a good idea writing the log to a text file, if I develop another version of this logging class I will add the option in the config.php file to chose between writing to a file or to a database.
    Thanks for the ideas

  7. my only gripe is that you are not using php5 PDO object for database usage. For a simple logging class I don’t see why you make a custom database class and wrap only mysql_* functions.

    -dg

  8. The only reason I created a mysql class to handle the connection and queries to the database was to make it easier to build on the logging class, so that adding complex mysql queries and functions would be easier, and to separate the logging class from the sql functions too, so that it is possible to use other SQL libraries other than MySQL.

    If you make a better version, or a PHP5 version of this class it would be great if you emailed it to me to add it here for other users, so if you do just tell me and I’ll be more than glad to add you here!

    1. Well I had this website where many users where trying to hack my admin panel, and the worrying part was that some actually managed, so I first wanted to create an IP blacklist, and then when I did the IP logging I decided to extend it to log everything I could, to know what was going on at my site.
      And the result was this ;)

  9. I like it very much. There seems to be a typo, though, in the version I got (240508).

    Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in logging/log.class.php on line 105

    Thanks a lot!
    Tom

    1. I am not sure what the problem might be, there is a demo page where I have it installed and working, and I’ve never found any problems.
      I’ll check the code to see if there is any sort of problem that might appear in weird cases I haven’t tested yet…

      Thanks for the info!
      Alex

  10. I will try to add it before the end of this week, since at this moment I’m quite busy.
    If you already did it, or are going to do it and you want to share it, attach it in an email to me ([email protected]) and I will update it in the article with the appropriate links and credit.

    Thanks for the idea

Leave a Reply