logging - How to put custom messages into a file in PHP -
i sending messages server through php
file. need have file record of passed time stamp through php
later references , bug fixes. used following code nothing changed in file. issue if use wamp
server in local machine? have attached sample code here!any guide on correct way achieve little new php.
<?php header('access-control-allow-origin: *'); header('access-control-allow-headers: origin, x-requested-with, content-type, accept'); header('access-control-allow-methods: post, get, options, delete'); header('access-control-max-age: 3600'); //include files-------------------------------------------------------------------------------------------- include_once("config.php"); //functions set class---------------------------------------------------------------------------------------- class converter { //log file------------------------------ const general_log_dir = '/log.txt'; function logmsg($msg,$num) { $date = date('d.m.y h:i:s'); $log = $msg." | date: ".$date."\n"; error_log($msg." | message: ".$date.$num, 3, self::general_log_dir); } } $message2="some text"; $num=11; $obj=new converter; set_time_limit(0); /* create tcp/ip socket. */ $socket = socket_create(af_inet, sock_stream, sol_tcp); if($socket) echo "<br>successfully socket created<br>"; else echo "<br>cannot create socket<br>"; $connect=socket_connect($socket,$svraddress,$port); if($connect) echo "<br>successfully connected server<br>"; else echo "<br>cannot connect server<br>"; $write=socket_write($socket,$message2); $obj->logmsg($message2,$num); echo '<h3 style="color:red">'.$message2."</h3></br>"; echo ' send on '.date("y:m:d:h:i:s").'</br>'; echo "closing socket..."; socket_close($socket); echo "finished.<br /><br />"; echo "{\"success\": true, \"message\": \"ok\"}"; ?>
did these using following methods got work done still don't know happened in previous case.still new php hope has same issue
method1
$file = fopen("text.log","a"); echo fwrite($file,$msg."\n"); fclose($file);
method2
// open file existing content $current = file_get_contents($file); $current .= $msg; $current.="\n"; */ // write contents file file_put_contents('text.log', $current . php_eol, file_append);
Comments
Post a Comment