Sunday, October 18, 2015

Console API or How to write better logs in the JavaScript Web Application

Console API

Preface

I've started to create a JavaScript Web Application. It is a frontend of a Java Application. These are totally decoupled. It's my first major experience of creating JavaScript application. During the development, I learned a lot about JavaScript language and its concepts. One of them was how to write better logs for debugging and finding clues of errors. I use log4J in Java backend application and I need an alternative for frontend JavaScript application.

Console API

I was familiar with console.log() but it wasn't enough for logging all states. After searching for better solutions, I found out that there are several Console API  for logging in JavaScript [1],[2],[3].
The Console API is non-standard. Do not use it on production sites facing the Web. It will not work for every user.
The console object provides access to the browser's debugging console. The specifics of how it works vary from browser to browser, but there is a de facto set of features that are typically provided.

Methods

There are several methods for logging such as log(), debug() and etc. I mainly use some of them which I'm going introduce them.

console.log(object[, object, ...])

Writes a message to the console. You may pass as many arguments as you'd like, and they will be joined together in a space-delimited line.
console.log('This is a sample log');

console.debug(object[, object, ...])

Writes a message to the console, including a hyperlink to the line where it was called.
console.debug('I am debug message');

console.info(object[, object, ...])

Writes a message to the console with the visual "info" icon and color coding and a hyperlink to the line where it was called.
console.info('I just want to share info!');

console.warn(object[, object, ...])

Writes a message to the console with the visual "warning" icon and color coding and a hyperlink to the line where it was called.
console.warn('Be quite! Big brother is watching you!');

console.error(object[, object, ...])

Writes a message to the console with the visual "error" icon and color coding and a hyperlink to the line where it was called.
console.error('Something goes wrong!');

console.assert(expression[, object, ...])

Tests that an expression is true. If not, it will write a message to the console and throw an exception.
console.assert((2 + 3) === 5); // Assertation true console.assert((2 * 1 * 0) === 3, 'It should be 0!'); // Assertation faild

console.clear()

Clears the console.
console.clear();

console.dir(object)

Prints an interactive listing of all properties of the object.
console.dir(document.body);

All Console Web APIs in one place

I've created a simple reference of Console API in github. You can see them at saeidzebardast.github.io/all-in-one-console-api/.

Sunday, August 2, 2015

Introduction to Redis

Preface

I like to try new technologies in software development. Recently, I've needed to save monitoring stats of our Threat Analyzer software. I required a database which should be very fast and easy to use in any platforms. After researching for this requirement I found Redis. I've used it for monitoring engine and messaging framework.

Introduction to Redis

I created a presentation about Redis to introduce it to my colleagues in Datis Pars.
Its agenda is:
  • What is Redis?
  • Features
  • Persistence
  • Data types
  • Commands
  • Publish / Subscribe
  • Sort
  • Transactions
  • Replication
  • Cluster

Thursday, March 26, 2015

Reset MySQL root password

MySQL

Preface

In some circumstances you need to reset MySQL root without knowing current password of it. For example you forget the password. You can change the password of the root user easily. The following guide is tested on Ubuntu 12.04 based on MySQL 5.5.

Reset root password (without knowing the current password)

To do so, please follow the instructions below:

Step 1: Stop the MySQL Service

At first, you need to stop the MySQL service:
$ sudo /etc/init.d/mysql stop

Step 2: Start the MySQL daemon in safe mode with skipping grants

Please run the MySQL in safe mode with skipping grants parameter:
$ sudo mysqld_safe --skip-grant-tables &

Step 3: run the MySQL prompt as root user without password

Now, you can enter the MySQL command line as root user without password:
$ mysql -u root

Step 4: Update the password of root user

Run the following commands to update root user password. Please replace "NEW-ROOT-PASSWORD" with the desired password:
mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

Step 5: Restart the MySQL service

You should restart the MySQL service to check the new password:
$ sudo /etc/init.d/mysql stop
$ sudo /etc/init.d/mysql start

Step 6: Login with the new password

Now, login to the MySQL prompt with the new password:
$ mysql -u root -p

Saturday, February 7, 2015

MySQL and Java workshop

Saeid Zebardast at workshop
Me at workshop

Preface

A few weeks ago, there was a Ubuntu 14.10 release party in Tehran, Iran. I had a workshop about developing software with MySQL and Java. Nearly 70 people were attended.
The workshop had three part:
  1. MySQL for beginners
  2. Java for beginners
  3. Developing software with MySQL and Java
Also, I create cheat sheets for Java and MySQL for participants.  I'm going to publish the presentations and cheat sheets in this post.

MySQL for beginners

The agenda for this presentation is:
  • Introduction
  • Installation
  • Execute SQL Statements
  • SQL Language
  • Some of The Most Important SQL Commands
  • SQL Syntax
  • Data Types
  • Operators
  • Some exercises

Java for beginners

The agenda for this presentation is:
  • Introduction
  • JVM
  • Java Principles
  • Java History
  • Java Uses
  • Installation
  • Java Syntax
  • Data Types
  • Variables
  • Operators
  • Select Statements
  • Iteration Statements
  • Comments Syntax
  • Access modifiers
  • Methods
  • Classes
  • Inheritance
  • Interfaces
  • Abstract
  • Java File Structure
  • Simple exercise


Developing software with MySQL and Java

The agenda for this presentation is:

  • Requirements
  • MySQL Data Definition
  • Java Classes
  • Methods for PhonebookApp
  • Compile and Run

MySQL Cheat Sheet


Java Cheat Sheet


Photos

Saeid Zebardast - MySQL and Java workshop
Saeid Zebardast - MySQL and Java workshop

MySQL and Java workshop
MySQL and Java workshop

Saeid Zebardast - MySQL and Java workshop
Saeid Zebardast - MySQL and Java workshop

MySQL and Java workshop
MySQL and Java workshop

Ubuntu 14.10 Release Party
Ubuntu 14.10 Release Party