Need Advice? CALL: +61 417949565

Riksi - web design and development

  • Home
  • ABOUT US
  • OUR WORK
  • BLOG
  • CONTACT US
  • Home
  • BLOG
  • Web developing
  • PHP
  • PHP – Create a customised log function

PHP – Create a customised log function

PHP – Create a customised log function

Ever wondered to have a customised log function to get array or text as input and add it to file with the exact date and time?

guess what? here is the function, you can add it to your config file( or in WordPress add it to functions.php) and call it anywhere in your code to log the file.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
 
<?php
 
function riksi_log($input_msg){
    $log_text="";
 
    //get the event occur date time,when it will happened
    $logData['log_datetime']='['.date('D Y-m-d h:i:s A').'] [client '.$_SERVER['REMOTE_ADDR'].']';
 
    //if message is array type
    if(is_array($input_msg)){   //concatenate msg with datetime
        foreach($input_msg as $msg)
            $log_text.=$logData['log_datetime']." ".$msg."\r\n";
    }else{
        //concatenate msg with datetime
        $log_text.=$logData['log_datetime']." ".$input_msg."\r\n";
    }
        //create file with current date name
        $logName='kobelog_'.date('Ymd').'.txt';
 
        //open the file in append mode
        $fHandler=fopen($logName,'a+');
 
        //write the info into the file
        fwrite($fHandler,$log_text);
 
        //close handler
        fclose($fHandler);
}
?>
 
 

its usefull if you want to test your code and see the content of the array, othe functions passed.

simply call the function like:

PHP
1
2
3
4
5
 
 
riksi_log($input_msg)
 
 

 

Tagged under: log, php
Nick
Monday, 31 December 2018 / Published in PHP, Web developing

What you can read next

Show WordPress Page Content in Another Post Template
Update PHP Memory Limit
Migrate WordPress server from Local to Live

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archive

  • General
    • Smart Home
  • Web developing
    • command line
    • CSS
    • General
    • git
    • Javascript
    • Laravel
    • Magento
    • MySQL
    • PHP
    • Security
    • Shopify
    • Wordpress

Recent Posts

  • Install HomeAssistant on Raspberry pi

  • Shopify ERR_INVALID_ARG_TYPE Error

Riksi

Riksi is a creative, professional & cutting-edge custom web design and development company.

HERE TO HELP

  • BLOG
  • Contact Us
  • Our Work
  • About Us

New on Blog

  • Install HomeAssistant on Raspberry pi

    To install it, We need to connect the SD card t...
  • Shopify ERR_INVALID_ARG_TYPE Error

    fix the Internal Server Error occurred while in...

GET IN TOUCH

T: (+61) 417 949 565
Email: [email protected]

Contact Us
Fill out the form and our team will be in touch with you promptly. Thank you for your interest!

AU & NZ
T: (+613) 9014 9495
T: (+61) 417 949 565
Email: [email protected]


US & Canada
Email: [email protected]

NAME

EMAIL

PROJECT TYPE
PROJECT BUDGET

MESSAGE

Could not authenticate you.

Whats on Instagram

Loading...

Tags

.htaccess calculation cherry pick composer content css deployer documentation error flex general knowledge get started git git stash hack javascript log mysql package password php PHP Trick replace scss shopify sidebar ssh symlink sync trick ubuntu windows 10 wordpress trick wp
  • GET SOCIAL
Riksi - web design and development

© 2020 All rights reserved. Riksi.com.

TOP