The Urbano A. Company’s Blog
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 (1882 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!
| Print article | This entry was posted by alex on March 21, 2008 at 8:08 pm, and is filed under General talk, PHP, Programming, Web-related. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
New threat for all Joomla and WordPress installations
about 5 months ago - 2 comments
There is a new BOT out there, and one of the bad ones. I have started receiving traffic from it in my servers over the past week, and after some investigation it turns out it is quite a powerful bot, and so simple to use even a kid with a computer could use it.
The bot More >
Parse links in user comments
about 5 months ago - No comments
When you allow users to comment and post stuff to your website, it is interesting and useful allowing them to post links and other stuff. But how can we do so easily?
Surely there is BBCode, phpBB, allowing only some HTML tags… etc but how easy is this approach for the end user? Of course some More >
Calculate age in PHP from timestamp
about 6 months ago - No comments
If you ever wanted to calculate someone’s age in PHP from a birth timestamp, you must take into account that the age is more than the number of years, since days and months are also important, so I wrote a simple function that will return the exact age for a given timestamp:
function getAge($birth){
$t = time();
$age More >
Easiest PHP file upload
about 7 months ago - No comments
Hello people,
I want to share with all of you a file upload class I have developed, that makes it stupid simple to upload files haha
The PHP class:
First of all, here is the PHP class you will need:
< ?php
//Uploader class, by Alex
// This class is meant to handle all kinds of file uploads for DJs Music
// More >
Easily assign an image to a post in WordPress
about 1 year ago - 1 comment
Have you ever wondered how to assign an image to a certain post using WordPress? Surely there are some plugins that try to do this, and maybe they accomplish it, but probably slowing down your blog.
Well, here is a way of doing it without slowing the blog or installing any sort of additional plugins. When More >
Did you mean… ? In php
about 1 year ago - 13 comments
In a new website I am developing for a client I had to add the usual “Did you mean… ?” in the search results for her. Si I started thinking for the easiest way to do this.
There are actually a lot of php functions out there to look for similar text. The most obvious one?
similar_text()
You More >
How we became web developers…
about 1 year ago - 3 comments
I normally write to those web developers/programmers who are already good, experienced, and thus the articles are somewhat advanced.
But today I got up feeling nostalgic I guess, or I just felt like remembering back on my www birth, on my first impression of the Internet, my first site online, my first steps in w3c standards, More >
Using Gravatars in your blog comments
about 1 year ago - No comments
I’m sure you have seen in some blogs or websites that as soon as you enter your email, or other people do, an avatar is displayed for them. If you haven’t still set your own you should do that now.
A Gravatar is a “Globally Recognized Avatar”, meaning that anyone can display yours if they know More >
PHP easy image editing:
about 1 year ago - 3 comments
Do you have a picture upload and you don’t know how to easily resize/edit the uploaded images?
Well here is a solution for php that will make your life really easy!
It is called Asido, so you may go and download their code, to follow this tutorial.
First of all I’ll suppose you already know how to upload More >
Visitor’s language detection in php
about 1 year ago - No comments
Is your site translated into more than one language? In case it is, you will find this topic really interesting, since it will allow you to automatically adapt your site’s language to the user’s.
In case your site is not translated, you can always use this to know the visitor’s language… I’m sure you’ll be able More >
about 1 year ago
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’?
about 1 year ago
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…
about 1 year ago
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
about 1 year ago
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
about 1 year ago
its great
http://www.w3answers.com
about 1 year ago
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
about 1 year ago
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
about 1 year ago
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
about 1 year ago
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!
about 1 year ago
Wow, how did you come up with something like this? Great post!
about 1 year ago
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
about 1 year ago
its very helpfull class
enjoy!!!!!!!!!!!! http://www.w3answers.com
about 1 year ago
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!
about 1 year ago
Hope you find it easy to use
If you run into any problems just comment and I’ll help you
about 1 year ago
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
about 1 year ago
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
about 1 year ago
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!
about 1 year ago
Is it possible to get a pagination for the log?
I know some ways that can be done for it.
about 1 year ago
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
about 1 year ago
Just download lets see how it goes
Thanks.