Jump to content

T13: Introduction to Simplog, lightweight logging


Recommended Posts

Posted (edited)

(Short introduction to yet another tool, blablabla.)

 

No more commenting out debug logs, no more explicit conditional logging, no more spamming your users with debug logs they have no use for.

 

Log to the osbot console, a file or any other log processor you can think of.

 

Log processors automatically filter out log objects that do not meet the processor's levlel requirement.

Each log processor can have its own requirements, you might not want to spam the user with debug or trace messages in the console, but storing those messages in a file (where they don't harass the user) could be a valuable practice.

 

Code example:

@ScriptManifest(author = "Botre", info = "", logo = "", name = "Simplog Example", version = 0)
public class ExampleScript extends Script {

	private Logger logger;
	
	public static final int LOG_USER = 100;
	public static final int LOG_DEV_DEBUG = 10;
	public static final int LOG_DEV_TRACE = 1;
	
	@Override
	public void onStart() throws InterruptedException {
		super.onStart();
		/*
		 * Set-up
		 */
		logger = new Logger();
		logger.addProcessor(new OSBotLogProcessor(this, LOG_USER));
		logger.addProcessor(new TextFileLogProcessor(new File("C:/Users/bjorn/Desktop/TextTest.txt"), LOG_DEV_TRACE));
		logger.start();
		logger.log("Welcome user", LOG_USER);
		logger.log("Exit onStart", LOG_DEV_TRACE);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public int onLoop() throws InterruptedException {
		logger.log("Enter onLoop", LOG_DEV_TRACE);
		logger.log("Started npc filtering", LOG_DEV_TRACE);
		List<NPC> goblins = getNpcs().filter(npc -> npc.exists() && npc.getName().equals("Goblin"));
		logger.log("Finished npc filtering", LOG_DEV_TRACE);
		logger.log("Goblins found: " + goblins.size(), LOG_DEV_DEBUG);
		logger.log("Exit onLoop", LOG_DEV_TRACE);
		return 500;
	}
	
	@Override
	public void onExit() throws InterruptedException {
		super.onExit();
		logger.log("Bye user", LOG_USER);
		logger.log("Exit onExit", LOG_DEV_TRACE);
		logger.stop();
	}

}

OSBot log:

[INFO][Bot #1][03/04 05:54:17 PM]: Started script : Simplog Example
[INFO][Bot #1][03/04 05:54:28 PM]: Welcome user
[INFO][Bot #1][03/04 05:54:27 PM]: Terminating script Simplog Example...
[INFO][Bot #1][03/04 05:54:27 PM]: Bye user
[INFO][Bot #1][03/04 05:54:27 PM]: Script Simplog Example has exited!

File log:

[2016-03-04T16:54:17Z] LOG SESSION STARTED
	[2016-03-04T16:54:17Z] Welcome user
	[2016-03-04T16:54:17Z] Exit onStart
	[2016-03-04T16:54:17Z] Enter onLoop
	[2016-03-04T16:54:17Z] Started npc filtering
	[2016-03-04T16:54:17Z] Finished npc filtering
	[2016-03-04T16:54:17Z] Goblins found: 3
	[2016-03-04T16:54:17Z] Exit onLoop
	[2016-03-04T16:54:17Z] Enter onLoop
	[2016-03-04T16:54:17Z] Started npc filtering
	[2016-03-04T16:54:17Z] Finished npc filtering
	[2016-03-04T16:54:17Z] Goblins found: 4
	[2016-03-04T16:54:17Z] Exit onLoop
	[2016-03-04T16:54:18Z] Enter onLoop
	[2016-03-04T16:54:18Z] Started npc filtering
	[2016-03-04T16:54:18Z] Finished npc filtering
	[2016-03-04T16:54:18Z] Goblins found: 4
	[2016-03-04T16:54:18Z] Exit onLoop
	[2016-03-04T16:54:18Z] Enter onLoop
	[2016-03-04T16:54:18Z] Started npc filtering
	[2016-03-04T16:54:18Z] Finished npc filtering
	[2016-03-04T16:54:18Z] Goblins found: 4
	[2016-03-04T16:54:18Z] Exit onLoop
	[2016-03-04T16:54:19Z] Enter onLoop
	[2016-03-04T16:54:19Z] Started npc filtering
	[2016-03-04T16:54:19Z] Finished npc filtering
	[2016-03-04T16:54:19Z] Goblins found: 4
	[2016-03-04T16:54:19Z] Exit onLoop
	[2016-03-04T16:54:19Z] Enter onLoop
	[2016-03-04T16:54:19Z] Started npc filtering
	[2016-03-04T16:54:19Z] Finished npc filtering
	[2016-03-04T16:54:19Z] Goblins found: 4
	[2016-03-04T16:54:19Z] Exit onLoop
	[2016-03-04T16:54:20Z] Enter onLoop
	[2016-03-04T16:54:20Z] Started npc filtering
	[2016-03-04T16:54:20Z] Finished npc filtering
	[2016-03-04T16:54:20Z] Goblins found: 4
	[2016-03-04T16:54:20Z] Exit onLoop
	[2016-03-04T16:54:20Z] Enter onLoop
	[2016-03-04T16:54:20Z] Started npc filtering
	[2016-03-04T16:54:20Z] Finished npc filtering
	[2016-03-04T16:54:20Z] Goblins found: 4
	[2016-03-04T16:54:20Z] Exit onLoop
	[2016-03-04T16:54:21Z] Enter onLoop
	[2016-03-04T16:54:21Z] Started npc filtering
	[2016-03-04T16:54:21Z] Finished npc filtering
	[2016-03-04T16:54:21Z] Goblins found: 4
	[2016-03-04T16:54:21Z] Exit onLoop
	[2016-03-04T16:54:21Z] Enter onLoop
	[2016-03-04T16:54:21Z] Started npc filtering
	[2016-03-04T16:54:21Z] Finished npc filtering
	[2016-03-04T16:54:21Z] Goblins found: 4
	[2016-03-04T16:54:21Z] Exit onLoop
	[2016-03-04T16:54:22Z] Enter onLoop
	[2016-03-04T16:54:22Z] Started npc filtering
	[2016-03-04T16:54:22Z] Finished npc filtering
	[2016-03-04T16:54:22Z] Goblins found: 4
	[2016-03-04T16:54:22Z] Exit onLoop
	[2016-03-04T16:54:22Z] Enter onLoop
	[2016-03-04T16:54:22Z] Started npc filtering
	[2016-03-04T16:54:22Z] Finished npc filtering
	[2016-03-04T16:54:22Z] Goblins found: 4
	[2016-03-04T16:54:22Z] Exit onLoop
	[2016-03-04T16:54:23Z] Enter onLoop
	[2016-03-04T16:54:23Z] Started npc filtering
	[2016-03-04T16:54:23Z] Finished npc filtering
	[2016-03-04T16:54:23Z] Goblins found: 4
	[2016-03-04T16:54:23Z] Exit onLoop
	[2016-03-04T16:54:24Z] Enter onLoop
	[2016-03-04T16:54:24Z] Started npc filtering
	[2016-03-04T16:54:24Z] Finished npc filtering
	[2016-03-04T16:54:24Z] Goblins found: 4
	[2016-03-04T16:54:24Z] Exit onLoop
	[2016-03-04T16:54:24Z] Enter onLoop
	[2016-03-04T16:54:24Z] Started npc filtering
	[2016-03-04T16:54:24Z] Finished npc filtering
	[2016-03-04T16:54:24Z] Goblins found: 4
	[2016-03-04T16:54:24Z] Exit onLoop
	[2016-03-04T16:54:25Z] Enter onLoop
	[2016-03-04T16:54:25Z] Started npc filtering
	[2016-03-04T16:54:25Z] Finished npc filtering
	[2016-03-04T16:54:25Z] Goblins found: 4
	[2016-03-04T16:54:25Z] Exit onLoop
	[2016-03-04T16:54:25Z] Enter onLoop
	[2016-03-04T16:54:25Z] Started npc filtering
	[2016-03-04T16:54:25Z] Finished npc filtering
	[2016-03-04T16:54:25Z] Goblins found: 4
	[2016-03-04T16:54:25Z] Exit onLoop
	[2016-03-04T16:54:26Z] Enter onLoop
	[2016-03-04T16:54:26Z] Started npc filtering
	[2016-03-04T16:54:26Z] Finished npc filtering
	[2016-03-04T16:54:26Z] Goblins found: 4
	[2016-03-04T16:54:26Z] Exit onLoop
	[2016-03-04T16:54:26Z] Enter onLoop
	[2016-03-04T16:54:26Z] Started npc filtering
	[2016-03-04T16:54:26Z] Finished npc filtering
	[2016-03-04T16:54:26Z] Goblins found: 4
	[2016-03-04T16:54:26Z] Exit onLoop
	[2016-03-04T16:54:27Z] Enter onLoop
	[2016-03-04T16:54:27Z] Started npc filtering
	[2016-03-04T16:54:27Z] Finished npc filtering
	[2016-03-04T16:54:27Z] Goblins found: 4
	[2016-03-04T16:54:27Z] Exit onLoop
	[2016-03-04T16:54:27Z] Bye user
	[2016-03-04T16:54:27Z] Exit onExit
[2016-03-04T16:54:27Z] LOG SESSION STOPPED

 

After changing the processor levels to:

		logger.addProcessor(new OSBotLogProcessor(this, LOG_DEV_DEBUG));
		logger.addProcessor(new TextFileLogProcessor(new File("C:/Users/bjorn/Desktop/TextTest.txt"), LOG_DEV_DEBUG));

OSBot log:

[INFO][Bot #1][03/04 05:58:54 PM]: Started script : Simplog Example
[INFO][Bot #1][03/04 05:58:54 PM]: Goblins found: 5
[INFO][Bot #1][03/04 05:58:55 PM]: Goblins found: 5
[INFO][Bot #1][03/04 05:58:55 PM]: Goblins found: 6
[INFO][Bot #1][03/04 05:58:56 PM]: Goblins found: 7
[INFO][Bot #1][03/04 05:58:56 PM]: Goblins found: 7
[INFO][Bot #1][03/04 05:58:57 PM]: Goblins found: 7
[INFO][Bot #1][03/04 05:58:57 PM]: Goblins found: 7
[INFO][Bot #1][03/04 05:58:58 PM]: Goblins found: 7
[INFO][Bot #1][03/04 05:58:58 PM]: Goblins found: 7
[INFO][Bot #1][03/04 05:58:59 PM]: Goblins found: 7
[INFO][Bot #1][03/04 05:58:59 PM]: Terminating script Simplog Example...
[INFO][Bot #1][03/04 05:58:59 PM]: Bye user
[INFO][Bot #1][03/04 05:58:59 PM]: Script Simplog Example has exited!

File log:

[2016-03-04T16:58:54Z] LOG SESSION STARTED
	[2016-03-04T16:58:54Z] Welcome user
	[2016-03-04T16:58:54Z] Goblins found: 5
	[2016-03-04T16:58:55Z] Goblins found: 5
	[2016-03-04T16:58:55Z] Goblins found: 6
	[2016-03-04T16:58:56Z] Goblins found: 7
	[2016-03-04T16:58:56Z] Goblins found: 7
	[2016-03-04T16:58:57Z] Goblins found: 7
	[2016-03-04T16:58:57Z] Goblins found: 7
	[2016-03-04T16:58:58Z] Goblins found: 7
	[2016-03-04T16:58:58Z] Goblins found: 7
	[2016-03-04T16:58:59Z] Goblins found: 7
	[2016-03-04T16:58:59Z] Bye user
[2016-03-04T16:58:59Z] LOG SESSION STOPPED
Edited by Botre
  • Like 2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...