A very long log was interrupted by the next log












1















I use a php function named "error_log" to write logs to a file, but sometimes a very long log was interrupted by the next log if the next log came very closely.



Here is an example:




Let's assume the first log looks like this: ^a{100000}$, and the
second log looks like this: bbbbbb, and then I use "error_log" to
write them into a log file almost simultaneously.




Here is how that log file looks like:




line 1:aaaaaaaaaaaaaaaaa...aaaaaaabbbbb



line 2:aaaaaaaaaaaaaa




As you can see the first log is interrupted by the second log when it is not fully written.



I wonder why that happend and how can I solve this problem.I thought about split the long log into two smaller logs, but how small?










share|improve this question





























    1















    I use a php function named "error_log" to write logs to a file, but sometimes a very long log was interrupted by the next log if the next log came very closely.



    Here is an example:




    Let's assume the first log looks like this: ^a{100000}$, and the
    second log looks like this: bbbbbb, and then I use "error_log" to
    write them into a log file almost simultaneously.




    Here is how that log file looks like:




    line 1:aaaaaaaaaaaaaaaaa...aaaaaaabbbbb



    line 2:aaaaaaaaaaaaaa




    As you can see the first log is interrupted by the second log when it is not fully written.



    I wonder why that happend and how can I solve this problem.I thought about split the long log into two smaller logs, but how small?










    share|improve this question



























      1












      1








      1








      I use a php function named "error_log" to write logs to a file, but sometimes a very long log was interrupted by the next log if the next log came very closely.



      Here is an example:




      Let's assume the first log looks like this: ^a{100000}$, and the
      second log looks like this: bbbbbb, and then I use "error_log" to
      write them into a log file almost simultaneously.




      Here is how that log file looks like:




      line 1:aaaaaaaaaaaaaaaaa...aaaaaaabbbbb



      line 2:aaaaaaaaaaaaaa




      As you can see the first log is interrupted by the second log when it is not fully written.



      I wonder why that happend and how can I solve this problem.I thought about split the long log into two smaller logs, but how small?










      share|improve this question
















      I use a php function named "error_log" to write logs to a file, but sometimes a very long log was interrupted by the next log if the next log came very closely.



      Here is an example:




      Let's assume the first log looks like this: ^a{100000}$, and the
      second log looks like this: bbbbbb, and then I use "error_log" to
      write them into a log file almost simultaneously.




      Here is how that log file looks like:




      line 1:aaaaaaaaaaaaaaaaa...aaaaaaabbbbb



      line 2:aaaaaaaaaaaaaa




      As you can see the first log is interrupted by the second log when it is not fully written.



      I wonder why that happend and how can I solve this problem.I thought about split the long log into two smaller logs, but how small?







      php text-files error-log






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 9:19









      Sanu0786

      550715




      550715










      asked Nov 22 '18 at 8:46









      zpwannaflyzpwannafly

      84




      84
























          1 Answer
          1






          active

          oldest

          votes


















          0














          In your case, since two simultaneous requests are made to write to same file, you should use asynchronous processing to queue the task.In queuing the process, no simultaneous attempts will be made to write the same file.
          The second task will start only upon the completion of first.



          For Async processing you can use frameworks like RabbitMq or Gearman.



          Another option is to uses databases to store your logs. Databases are designed to avoid such type of conflicts.



          Hope either of these will solve your problem.



          Edit: To add further, you can use flock() to aquire a lock on any file, you wish to write. You can aquire an exclusive lock and block/enqueue the second operation to wait, until the lock is released by the first. Go through this link for more details.






          share|improve this answer


























          • Thank you for your help, but it seems a lot of work to do what you said considering this situation rarely happen and my log data is NOT that important. I was hoping the file system to provide me an atomic writing mechanism or something like that. Is there one?

            – zpwannafly
            Nov 22 '18 at 11:35













          • Please check the edited answer

            – Penguine
            Nov 28 '18 at 4:54











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426951%2fa-very-long-log-was-interrupted-by-the-next-log%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          In your case, since two simultaneous requests are made to write to same file, you should use asynchronous processing to queue the task.In queuing the process, no simultaneous attempts will be made to write the same file.
          The second task will start only upon the completion of first.



          For Async processing you can use frameworks like RabbitMq or Gearman.



          Another option is to uses databases to store your logs. Databases are designed to avoid such type of conflicts.



          Hope either of these will solve your problem.



          Edit: To add further, you can use flock() to aquire a lock on any file, you wish to write. You can aquire an exclusive lock and block/enqueue the second operation to wait, until the lock is released by the first. Go through this link for more details.






          share|improve this answer


























          • Thank you for your help, but it seems a lot of work to do what you said considering this situation rarely happen and my log data is NOT that important. I was hoping the file system to provide me an atomic writing mechanism or something like that. Is there one?

            – zpwannafly
            Nov 22 '18 at 11:35













          • Please check the edited answer

            – Penguine
            Nov 28 '18 at 4:54
















          0














          In your case, since two simultaneous requests are made to write to same file, you should use asynchronous processing to queue the task.In queuing the process, no simultaneous attempts will be made to write the same file.
          The second task will start only upon the completion of first.



          For Async processing you can use frameworks like RabbitMq or Gearman.



          Another option is to uses databases to store your logs. Databases are designed to avoid such type of conflicts.



          Hope either of these will solve your problem.



          Edit: To add further, you can use flock() to aquire a lock on any file, you wish to write. You can aquire an exclusive lock and block/enqueue the second operation to wait, until the lock is released by the first. Go through this link for more details.






          share|improve this answer


























          • Thank you for your help, but it seems a lot of work to do what you said considering this situation rarely happen and my log data is NOT that important. I was hoping the file system to provide me an atomic writing mechanism or something like that. Is there one?

            – zpwannafly
            Nov 22 '18 at 11:35













          • Please check the edited answer

            – Penguine
            Nov 28 '18 at 4:54














          0












          0








          0







          In your case, since two simultaneous requests are made to write to same file, you should use asynchronous processing to queue the task.In queuing the process, no simultaneous attempts will be made to write the same file.
          The second task will start only upon the completion of first.



          For Async processing you can use frameworks like RabbitMq or Gearman.



          Another option is to uses databases to store your logs. Databases are designed to avoid such type of conflicts.



          Hope either of these will solve your problem.



          Edit: To add further, you can use flock() to aquire a lock on any file, you wish to write. You can aquire an exclusive lock and block/enqueue the second operation to wait, until the lock is released by the first. Go through this link for more details.






          share|improve this answer















          In your case, since two simultaneous requests are made to write to same file, you should use asynchronous processing to queue the task.In queuing the process, no simultaneous attempts will be made to write the same file.
          The second task will start only upon the completion of first.



          For Async processing you can use frameworks like RabbitMq or Gearman.



          Another option is to uses databases to store your logs. Databases are designed to avoid such type of conflicts.



          Hope either of these will solve your problem.



          Edit: To add further, you can use flock() to aquire a lock on any file, you wish to write. You can aquire an exclusive lock and block/enqueue the second operation to wait, until the lock is released by the first. Go through this link for more details.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 28 '18 at 4:54

























          answered Nov 22 '18 at 8:59









          PenguinePenguine

          420414




          420414













          • Thank you for your help, but it seems a lot of work to do what you said considering this situation rarely happen and my log data is NOT that important. I was hoping the file system to provide me an atomic writing mechanism or something like that. Is there one?

            – zpwannafly
            Nov 22 '18 at 11:35













          • Please check the edited answer

            – Penguine
            Nov 28 '18 at 4:54



















          • Thank you for your help, but it seems a lot of work to do what you said considering this situation rarely happen and my log data is NOT that important. I was hoping the file system to provide me an atomic writing mechanism or something like that. Is there one?

            – zpwannafly
            Nov 22 '18 at 11:35













          • Please check the edited answer

            – Penguine
            Nov 28 '18 at 4:54

















          Thank you for your help, but it seems a lot of work to do what you said considering this situation rarely happen and my log data is NOT that important. I was hoping the file system to provide me an atomic writing mechanism or something like that. Is there one?

          – zpwannafly
          Nov 22 '18 at 11:35







          Thank you for your help, but it seems a lot of work to do what you said considering this situation rarely happen and my log data is NOT that important. I was hoping the file system to provide me an atomic writing mechanism or something like that. Is there one?

          – zpwannafly
          Nov 22 '18 at 11:35















          Please check the edited answer

          – Penguine
          Nov 28 '18 at 4:54





          Please check the edited answer

          – Penguine
          Nov 28 '18 at 4:54




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426951%2fa-very-long-log-was-interrupted-by-the-next-log%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini