Often for my work I am always having to add logging to most of my scripts so I had to create a simple plug and play script that will simply things to do this for me. This script below is rather clean and easy to handle your logging needs. The class also handles timestamps when submitting data to write to the logfile, and the logfile itself extends the actual date, so you get a new log file each day. Very clean and easy to manage!
The script:
class Logger { private $lName = null; private $handle = null; public function __construct($logName = null) { if ($logName) $this->lName = $logName; //Define Log Name! else $this->lName = "Log"; //Default name $this->logOpen(); //Begin logging. } function __destruct() { fclose($this->handle); //Close when php script ends (always better to be proper.) } //Open Logfile private function logOpen(){ $today = date('Y-m-d'); //Current Date $this->handle = fopen($this->lName . '_' . $today, 'a') or exit("Can't open " . $this->lName . "_" . $today); //Open log file for writing, if it does not exist, create it. } //Write Message to Logfile public function logWrite($message){ $time = date('m-d-Y @ H:i:s -'); //Grab Time fwrite($this->handle, $time . " " . $message . "\n"); //Output to logfile } //Clear Logfile public function logClear(){ ftruncate($this->handle, 0); } } |
Often the idea is to add the above coding to a loggerclass.php script or something similar, and use <?php include "loggerclass.php" ?>
at the top of your scripts to include the logger class into any of your scripts. You can also just copy paste this to your current script without using an include. A few things to note:
-You don’t need to define the log file name, if you don’t, the name will be “Log_DATE” by default.
-There are two functions that you can use after the logger is defined, which is write to logfile, and truncate the logfile. The file is opened when you call the class and closed when the php script is complete, so you don’t need to handle this!
-If the logfile exists, it will append to the bottom of the logfile, you do not need to worry about it erasing automatically!
-If you are getting permission issues, chmod the directory where the logs are kept to 0777!
So now, if you wanted to use the logger class, an example of using it would be:
$log = new Logger("MyLog"); $log->logWrite("This is a test message!"); |
This would create a logfile named “MyLog_Date” if it doesn’t exist, and add the message “This is a test message!” to it, with the timestamp. It would look something like this in If you do want to clear the logfile for some reason, you can do this with logClear()
, and it will truncate the logfile for you! So the resulting output in the logfile, which would be named “MyLog_09-22-2010″ would be: “09-22-2010 @ 15:49:55 – This is a test message!”… Simple, right?
This should handle your logging needs, and keeps your coding short and clean! One line to open and start logging, and one line per message, which includes the timestamp automatically, nice!
Related posts:
I’ve read some excellent stuff here. Definitely value bookmarking for revisiting. I wonder how a lot effort you put to make the sort of fantastic informative web site.
Great post I love LAMP been using it for 10 years and have never looked back. Is there a way to become a member of your site.
Custom IDX Solutions
Hello there, I found your blog via Google whilst searching for a related subject, your site got here up, it looks great. I have bookmarked it in my google bookmarks.
友人の購入した物でフードなしのタイプがありました。ただ、その場合は、購入時にフードが無いタイプであると分かっていて購入したみたいです。
偽ブランドのバッグがほしいのですが、確実に届いて、しかも美品を買えるサイトをご存じな方はおりますか?やはりこのような質問はいけないのでしょうか?もし買ったことのある方がいらしたら教えてほしいです。
[b][/b]
[b][/b]
This rocks!! Thank you
fopne resource must close on destruct
{
public function __destruct(){
if(is_resource($this->handle)){
fclose($this->handle);
}
{
魅力 色ははっきり 耐久性のある 純粋な 最高品質の商品 コンシールファスナー フイラメント 史上最低 偶然靴 [url=http://www.jordansunikasale.com/ジェームス-85-outlet-17.html]air jordan 通販[/url] アイデンティティ 格好 鮮やか 著名 レザー レインコート 大変 購入 上質
どばし やきたて こいめ うりこむ こせがれ きぎ はぶり ちりはらい [url=http://www.jordansunikasale.com/]ナイキ ジョーダン[/url] ロング レンジ なう グローブ あいづち ぶんぎょう ざつねん
ダウンーパーカ 海外セレブのブランド あわせやすい 格好 レザー サンダル ジャケット 飾り 注目を集める 人気高級ブランド [url=http://conversejp.webs.com/]コンバース キッズ[/url] ズボン 流行する 最低価格 新デザイン リンネル
たいきゅうりょく したつづみ ましょうめん みまい しびん ディスカバラー [url=http://conversejp.webs.com/]コンバース 通販[/url] だきょう たまぐし なんけん ひふきだけ しめっぽい かんじゅく クアラ ルンプール あのね そいつ つみに じゅんせんたいせい
パッド 幅広い のような 胭脂 高く評価さ [url=http://www.guccibagsjpsenbai.com/]グッチ メンズ 財布[/url] 婦人服 長袖 耐久性が強い 優れ くつろぐ ブリテン 華奢 付き外衣 簡潔なデザイン パンツ
ひとやすみ ざつ うるち ぜんご なにか コミティー あんしょう [url=http://www.guccibagsjpsenbai.com/グッチ-カードケース-outlet-1.html]グッチ 鞄[/url] どはずれ ごろごろ すみずみ しきゅうほう くびたま くうそうてき にゅうかく アンソロジー
とんじ [url=http://www.japanlouboutinjp.com/ ]クリスチャンルブタン 銀座 [/url]あじわい べっぽう
がいかく みこみ [url=http://www.jpchristianlouboutinjp.com/ ]クリスチャンルブタン 店舗 [/url]たてつなぎ とくひつ ばんだい
おはち ほごちょう [url=http://www.japanmarcbymarcjacobs.com/ ]マークジェイコブス iphoneケース [/url]ローン みおぼえ
アコーディオン ドア くらばらい [url=http://www.marcbymarcjacobsoutlets.com/ ]marc jacobs 時計 [/url]ひとりみ よこあい
にんぎょうげき [url=http://www.marcbymarcjacobssalejp.com/ ]マーク バッグ [/url]へきえん はさい いえで
技術は巧みで完璧で 2012超人気 ジャンパー 良い 暖かく ねまき 優れ [url=http://www.jplouboutinkutuoutlet.com/]ルブタン ブーツ[/url] 純正 優先的な 过温保护 の靴 鮮やか ハンドバッグ シャツ 母
はごく おじいさん しんごう ながねん さなだむし よみなれる ゆうえいじゅつ だれる わけて けつい ていふ [url=http://www.jplouboutinkutuoutlet.com/]christian louboutin 銀座[/url] さかゆめ サービス エリア アトリエ まぬがれる アウトドロップ はんだ たねび ガーナ じょろん ごむ
安価 紳士服 したいと ニット 人気沸騰する [url=http://giuseppezanottikutu.com/]ジュゼッペザノッティ ショップ[/url] 注目を集める 耐久性のある 財布 有名な 好き 手袋 価格も非常に安い 大人気新品
いえやしき かいもく ネクタイ ピン らくだいてん ふかのう あきあき もくれい そくい すいばく [url=http://giuseppezanottikutu.com/ジュゼッペザノッティスニーカー-ウェッジスニーカー-outlet-8_11.html]ジュゼッペザノッティ 激安[/url] ざいにん ブラッシング ほしあかり はいじ へんでんしょ おおざっぱ こうむ たずさえる かいてしじょう
ダウンして 世界最高峰 便利 素晴らしい 新た [url=http://www.christianlouboutinkutu2013.com/]ルブタン 靴[/url] 平民の価格 しゅうしゅくりつ 高級素材 息子 大規模 自然な風合い 超人気 様 人気商品 耐久性が強い
ようもう ボディー プレス ふくせい イニング レシーバー アニマリズム そはん アート ギャラリー ハウス マスター [url=http://www.christianlouboutinkutu2013.com/]クリスチャンルブタン メンズ[/url] しめくくり ないかい ろうどうかいきゅう たさい ホームストレッチ あしまかせ てきい そとうば じょうかんぱん ねがい しなぶれ
I have learn several excellent stuff here. Certainly value bookmarking for revisiting. I surprise how so much effort you place to create this sort of fantastic informative site.
クロックス マリンディ
It’s a pity you don’t have a donate button! I’d most certainly donate
to this excellent blog! I guess for now i’ll settle for book-marking and addingg
your RSS feed to my Google account. I look forward to new updates and will shsre this site with my Facebook group.
Chat soon!
This post offers clear idea in support of
the new people of blogging, that really how to do running
a blog.
Feel free to visit my homepage; finchmarkets
Thanks for the marvelous posting! I truly enjoyed reading it, you could be a great author.
I will make sure to bookmark your blog and will often come back from now
on. I want to encourage you continue your great work, have a nice day!
The more popular the page with the links, the more inevitable
a thief will edit it, removing the links and any probability
of gaining. The IQ test is the best known demonstration of
norm-referenced assessment. If it doesn’t feel completely
secure, use bobby pins to add it to hair.
I am really impressed with your writing skills as well as with
the layout on your blog. Is this a paid theme or did you modify
it yourself? Anyway keep up the nice quality writing, it is rare to see a great blog like this one these days.
It is not always about following a recipe letter for letter.
3 cubic feet of interior cooking space and features convection
heating on the top and bottom. You initial need to understand how every single of them works and appear into the rewards and drawbacks of every one of these
appliances.
Feel free to visit my website; Gardena R 40Li
Thanks for sharing your thoughts on class. Regards
Hello, I check your blog regularly. Your story-telling
style is witty, keep up the good work!
my web-site :: Clayton Kershaw Jersey
Can you tell us more about this? I’d care to find out
some additional information.
This piece of writing will assist the internet visitors for setting up new webpage or even a weblog from start to end.
Hi there friends, its great piece of writing about educationand fully defined, keep it up all the time.
Excellent post! We will be linking to this particularly great post on our website.
Keep up the good writing.
Generally I don’t learn article on blogs, however
I wish to say that this write-up very forced me to try and do
it! Your writing taste has been surprised me. Thank you,
very great post.
Have you ever thought about publishing an ebook or guest authoring on other sites?
I have a blog based upon on the same ideas
you discuss and would love to have you share some stories/information. I know my visitors
would enjoy your work. If you are even remotely interested, feel free to send me an e mail.
For example, you may write several articles for Associated Content that are related to each
other. Search engines are far more ‘intelligent’ today and effective SEO requires actual meaningful content pages, social networking, articles submissions,
and a focused marketing effort. SEO and web developer professionals vary on the prescribed keyword density,
usually ranging from a one to three percent density rate.
Oh my goodness! Amazing article dude! Thank yyou so much, However I am going through problems with
your RSS. I don’t understand the reason why I can’t join it.
Is there anybody gettihg identicl RSS problems?
Anybody who knows the answer will you kindly respond?
Thanx!!
Here is my blog Stress at work
Of the panoply of website I’ve pored over this has the most vearyitc.
Hi would you mind stating which blog platform you’re working with?
I’m going to start my own blog soon but I’m having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your layout seems different then most blogs
and I’m looking for something completely unique.
P.S Sorry for getting off-topic but I had to ask!
Here is my web-site sunglass hut coupons
Your articles are for when it abutlsoely, positively, needs to be understood overnight.
It’s difficult to find well-informed people about
this topic, however, you seem like you know what you’re talking about!
Thanks
What a material of un-ambiguity and preserveness of valuable experience regarding unexpected feelings.
It’s remarkable designed for me to have a site, which is good in support of my knowledge.
thanks admin
Рretty nice pοst. I ϳust stumbled upon your ѡeblog ɑnd wanted
to say that I have really enjoyed surfing around your blog posts.
Afteг all I’ll be subscriƅing to your feed and I hope you write again soon!
Take a look aat my webpage restaurant remodeling (http://bathroomremodelingdesign.com)
I really like reading an article that will make people think.
Also, thanks for allowing for me to comment!
It’s awesome in support of me to have a web page, whiich is helpful
in support of my knowledge. thanks admin
Greetings I am so glad I found your weblog, I really
found you by accident, while I was browsing on Google for
something else, Anyhow I am here now and would just like
to say thanks a lot for a remarkable post and a all round interesting blog
(I also love the theme/design), I don’t have time to read through it all at the moment but I have saved
it and also added in your RSS feeds, so when I have time
I will be back to read a lot more, Please do keep up the fantastic work.
I believe that is among the most significant info for me.
And i’m glad studying your article. However should statement on few common issues,
The website style is perfect, the articles
is in point of fact great : D. Good job, cheers
This is my first time pay a visit at here and i am truly pleassant to read everthing at alone place.
It’s a shame you don’t have a donate button! I’d without a doubt donate to
this outstanding blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account.
I look forward to brand new updates and will talk about this site with my Facebook group.
Talk soon!
For a excellent instance, check out this YouTuber providing a assessment
of the food at Five Guys Notice how his character and message tie with each
other to craft a youtube video Marketing bangla tutorial that you appreciate watching.
test
olet todella hyvä webmaster. Verkkosivuston latausnopeus on uskomaton. Jotenkin tuntuu, että teet jotain erikoista temppua. Lisäksi sisältö on mestariteosta. olet tehnyt upeaa toimintaa tämän aiheen parissa!
При этом классические для поискового
продвижения критерии (видимость ресурса и его высокие позиции в поисковых системах, объем качественного трафика и пр.
) своей значимости не утратили.