日志文件PHP [PHP]

时间:2016-12-28 08:35:53

标签: php logfile

我目前正在尝试做一个日志文件,我想插入一个反向的年代表样式(最新的项目将放在所有现有文本列表的顶部,以前的现有项目将被推送到下面),想想它就像Facebook页面,如果您访问个人资料页面,您可以通过最新的方式查看他的所有帖子 - >最老的,而不是我现在拥有的,最古老的 - >最新。到目前为止,这是我的代码;

    $date = date("d-m-Y H:i:s");
    $file = 'logfile.txt';
    if (file_exists($file)) {
        $current = file_get_contents($file);
        $current .= PHP_EOL.$date;
        file_put_contents($file, $current);
    } else {
        file_put_contents($file, $date);
    }  

1 个答案:

答案 0 :(得分:0)

您要将日期字符串添加到变量的末尾:

here is my logcat:

2-28 14:11:44.201 3245-3504/com.example.zeba.broccoli E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                                                                         Process: com.example.zeba.broccoli, PID: 3245
                                                                         java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                             at android.os.AsyncTask$3.done(AsyncTask.java:304)
                                                                             at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                             at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                             at java.lang.Thread.run(Thread.java:818)
                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
                                                                             at com.example.zeba.broccoli.RegistrationForm$CreateNewProduct.doInBackground(RegistrationForm.java:147)
                                                                             at com.example.zeba.broccoli.RegistrationForm$CreateNewProduct.doInBackground(RegistrationForm.java:107)
                                                                             at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                             at java.lang.Thread.run(Thread.java:818) 
12-28 14:11:44.341 3245-3354/com.example.zeba.broccoli D/mali_winsys: new_window_surface returns 0x3000,  [540x960]-format:1
12-28 14:11:44.456 3245-3245/com.example.zeba.broccoli I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2f0f4385 time:209884695
12-28 14:11:44.736 3245-3245/com.example.zeba.broccoli E/WindowManager: android.view.WindowLeaked: Activity com.example.zeba.broccoli.RegistrationForm has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{6dda29d V.E..... R......D 0,0-501,174} that was originally added here

您需要从最新开始并在其后添加旧条目:

$current .= PHP_EOL.$date;