more G-Labs products

Author Topic: How to make my own Log  (Read 1707 times)

June 19, 2016, 10:46:25 PM
Read 1707 times

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
I have looked at BKENOBI's Log() function a bit but still had a question.

If I want to make my own log...  one that is not cluttered up with other log messages...  How would I best do this?  Can I simply open a file and fprint to it or is there a better way?

Thanks for any advice.

June 20, 2016, 05:33:14 PM
Reply #1

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
You can use the log function and modify it to write whatever you want.  I wrote the date/time plus whatever I wanted logged.  If you want more/less, you can do that.  You could also just use the raw code from the log function to write to a file, but I thought it was much cleaner to create a function.

June 20, 2016, 06:56:59 PM
Reply #2

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
In order to use your log() function,  do I have to "enable" the program ?  I am not sure how the function name space works.

Also-  where would I find the native log() code you mention?

Thanks!

June 20, 2016, 09:09:13 PM
Reply #3

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I didn't mean to imply the native logging capability.  However, there is a way to write to a Linux log file.  I've never done it that way but it is a possibility as well.  In order to use the log function I posted, you need to have it in your code that you want to enable logging in.  I have not been able to use code from one app from another so I always duplicate the log function for each app.

June 25, 2016, 01:40:23 AM
Reply #4

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
I did similar to this and filled the disk so I would also add log rotation.  However their is a command to use homegenie logging that is passed through to the nlog framework which would be a better option.  I just can't remember it off the top of my head

June 25, 2016, 06:38:30 PM
Reply #5

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196

I agree,  will be careful.  I just need some logging for a short time to figure out why my code is acting up.


June 26, 2016, 11:00:01 PM
Reply #6

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
here you go:

try this:

Code: [Select]
Program.Log.Trace("Sample trace message");
Program.Log.Debug("Sample debug message");
Program.Log.Info("Sample informational message");
Program.Log.Warn("Sample warning message");
Program.Log.Error("Sample error message");
Program.Log.Fatal("Sample fatal error message");

June 26, 2016, 11:48:44 PM
Reply #7

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
Thanks David,

And then what filename do these get written to?  Thanks!!

June 27, 2016, 01:23:45 AM
Reply #8

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
They get written to the main homegenie log file.
 /usr/local/bin/homegenie/log/

tail -f logname should help you

June 27, 2016, 04:37:09 PM
Reply #9

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I'll have to take a look at that approach.  I like being able to write log files as I want them, but it would be better to use the native system.

June 28, 2016, 03:16:46 PM
Reply #10

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
I copied your approach but as I said it bit me in the backside and I ran out of space and I didn't notice.. school boy error! I need to put some better monitoring in for my home systems :)

I'm now going to use this approach going forwards :)

What I do need to do though is look at the MIG code and see if I can configure what the 'http' server uses and maybe change the logging level of this / or make it configurable through arguments as the HG log is getting filled with a load of stuff that I don't care about :)

June 28, 2016, 04:19:34 PM
Reply #11

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I had mine set up so that it would write to a log that was located on a file server rather than on the RPi.  However, when I lost the SD card a while back (without having a backup of course), I couldn't figure out how to get the remote directory to map correctly at startup as I did originally.  As a result, HG would start prior to the mapping and start writing logs that would be local rather than remote.  It was enough of a PITA that I gave up and left it as is.  It's not a good idea to do this, but I did it anyway.