A very long log was interrupted by the next log











up vote
1
down vote

favorite












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




























    up vote
    1
    down vote

    favorite












    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


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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 at 9:19









      Sanu0786

      536315




      536315










      asked Nov 22 at 8:46









      zpwannafly

      63




      63
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          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 at 11:35












          • Please check the edited answer
            – Penguine
            Nov 28 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








          up vote
          0
          down vote













          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 at 11:35












          • Please check the edited answer
            – Penguine
            Nov 28 at 4:54















          up vote
          0
          down vote













          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 at 11:35












          • Please check the edited answer
            – Penguine
            Nov 28 at 4:54













          up vote
          0
          down vote










          up vote
          0
          down vote









          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 at 4:54

























          answered Nov 22 at 8:59









          Penguine

          378214




          378214












          • 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 at 11:35












          • Please check the edited answer
            – Penguine
            Nov 28 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 at 11:35












          • Please check the edited answer
            – Penguine
            Nov 28 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 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 at 11:35














          Please check the edited answer
          – Penguine
          Nov 28 at 4:54




          Please check the edited answer
          – Penguine
          Nov 28 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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







          Popular posts from this blog

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

          Berounka

          I want to find a topological embedding $f : X rightarrow Y$ and $g: Y rightarrow X$, yet $X$ is not...