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)

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
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:
< ?php //Initialize logging clases include('logInit.php'); // ?>
This is the initialization of the logging object and all other needed stuff.
The logging function is:
< ?php $log->logg('page','message','priority','class','mail'); ?>
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 (1843 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!










PHP logging class:…
This is a php class that will allow you to easily log any important activity in your site. It doesn’t require any programming skills as it is very easy to install and use….
Looks nice and clean. I would give this a try if I still used PHP.
One question though, why is the logging method named `logg’ and not just `log’?
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…
Okay, but since this is a method on your own class, that shouldn’t matter. You could name it `require_once’ for all PHP cares
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
PHP logging class…
[...]This is a php class that will allow you to easily log any important activity in your site. It doesn't require any programming skills as it is very easy to install and use.[...]…
[...] l’argomento delle risorse gratis trattato qualche giorno fa, segnalo una classe per i log degli avvenimenti del proprio sito. Si tratta di PHP Logging Class v.1.2 di cui vediamo [...]
its great
http://www.w3answers.com
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
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
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
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!
Urbano’s Blog » PHP logging class…
This class can be used to log activity to a database It can add activity entries to a log database table…
[...] PHP Logging Class
I’ve tinkered around with creating my own logging class but after seeing this I may not have to. If you’re PHP programmer, try out this awesome logging class. It could save you a lot of time down the road![...]
Wow, how did you come up with something like this? Great post!
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
its very helpfull class
enjoy!!!!!!!!!!!! http://www.w3answers.com
I like how you coded it, and I agree with aruma, a great post!
Downloaded it, now I’ll try to insert it in my site!
Hope you find it easy to use
If you run into any problems just comment and I’ll help you
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
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
One of the few good logging classes I could find.
And a fantastic one, i’m going to use this one for alot of my projects.
Thanks for this great script!
Is it possible to get a pagination for the log?
I know some ways that can be done for it.
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 (alejandro@urbanoalvarez.es) and I will update it in the article with the appropriate links and credit.
Thanks for the idea
Just download lets see how it goes
Thanks.